逐行读取文本文件并将每一行分配给C语言中的变量 [英] reading text file line by line and assigning each line to a variable in C language

查看:151
本文介绍了逐行读取文本文件并将每一行分配给C语言中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是编程新手,目前正在使用C语言进行项目.我想逐行读取文本文件的内容,并将每行的内容分配给C语言中的变量.有人可以帮我吗?

问候
Jawad


i''m new in programming and currently working on a project using C language. i want to read contents of a text file line by line and assign each line''s contents to a variable in C language. can anyone help me with this?

Regards
Jawad

推荐答案

这里有很多方法,很少提到:
http://www.mrx.net/c/program.html [
There are many ways and few ways are mentioned here:
http://www.mrx.net/c/program.html[^]


这是大多数新手的常见问题解答还不了解什么是编译器作业.

无法给出答案,因为很多问题都无法澄清.

1)文件内容是否遵循特定的格式或语法(即,内容是否可以预测")还是文件实际上可以包含任何内容?
从这个问题来看,假设文件包含以某种方式可识别的文本"分成行"似乎是正确的.
2)线路数量是否固定?一定数量的阅读后,您是否可以停止阅读?

这里的要点是,变量"(本地或全局:由名称标识的值)的概念是静态的:您在程序运行之前声明的内容.您不能将行存储在变量数量的变量中.

问题在于,需要使用动态结构,可以在程序运行时对其进行扩展(您不知道一行的长度是多少,那里有多少行),并且需要使用动态内存,并且需要使用指针(或在C ++中使用管理动态内存的类).

也就是说,在C和C ++中,问题可以用完全不同的方式解决(因此,您必须选择所需的语言:C/C ++是不存在的语言:C是C,C ++是C ++.就像您和您的兄弟拥有相同的姓氏,但您不是同一个人,而且并不总是可互换的!)

无论采用哪种解决方案,都不会最终将每行加载到 A (以他自己的名字...表示)的变量中,而是将整个文件加载到需要一些函数(或方法,或索引或迭代...)来访问行的内容,因为仅通过命名它们就无法访问它们:它们是在执行期间创建的,因此没有名称",由于名称是在设计时声明的,而在编译时是声明的,因此尚不知道文件".

现在很明显,您必须了解许多概念(主要是 vector string iteration 索引的概念加上 stream 的概念,以及-in C- array 动态分配释放的概念 i>和指针)不清楚是否已经拥有(我想您还不知道,否则您提出的问题的表述和措词会稍有不同).

我发现奇怪的是,许多新手都反对这个问题,从理论上讲,甚至不应该存在,因为这涉及到令人误解的要求和概念化:您该怎么做?读完这些行?
This is a FAQ of most every newbie that have not yet understood what is the compiler job.

The answer cannot be given because there are a lot of things the question doesn''t clarify.

1) Is the file content obeying to a particular format or syntax (i.e: is the content somehow "predictable") or can the file contain virtually anything?
From the question it seems correct to assume the file contains "text" diveded into "lines" somehow recognizable.
2) Is the number of line somehow fixed? May you stop reading after a given number of them or not?

The point, here, is understand that the concept of "variable" (local or global: a value identified by a name) is static: something you declare BEFORE the program runs. You cannot store lines in a variable amount of variables.

The problem, requires the use of dynamic structures, that can be expanded as the program runs (you don''t know how much a line is long and how many lines are there), and that requires the use of dynamic memory and that requires the use of pointers (or, in C++, the use of classes that manages dynamic memory).

That said, the problem is solvable in a completely different manner in C and in C++ (so you have to choose what you want: C/C++ is a language that doesn''t exist: C is C, C++ is C++. Just like you and your brother share the same surname, but you''re not the same person, and not always interchangeable!)

Whatever the solution will be, you will not end-up with each line loaded in A (in the sense of his own named ...) variable, but with the entire file loaded into a data structure requiring some functions (or methods, or indexing or iteration ...) to access the content of the lines, since they will not be accessible by just naming them: they are created during execution, so that don''t have "names", since names are declared at design time and -at compile time- no "file" is known yet.

It is now clear that you have to know a number of notions (mainly: the concept of vector, string and iteration or indexing plus the concept of stream, and -in C- the concept of array, dynamic allocation and deallocation and pointer) that is not clear if you already have it or not (I suppose you don''t, otherwise your question wold have have a slightly different formulation and wording).

What I found strage is that many newbies stuck against this problem, that -in theory- should even not exist, since it comes to a misleading requirement and conceptualization: what do you have to do with those lines, after you read them?


首先,请先寻找自己在Google上的答案.如果您发现有任何特定问题.
通用解决方案:在文件中查找新的留置字符作为字符串分隔符,然后将其填充到变量中.

让你的灵魂自由:d
First look yourself for the answer over Google. If you find any problem be specific to that.
General solution : look for new lien character as a string seprator in a file and fill it in variable.

Set your soul free :d


这篇关于逐行读取文本文件并将每一行分配给C语言中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆