Click here to Skip to main content
14,332,494 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi,
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
Posted
Updated 22-Jan-12 16:40pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Jan-12 23:36pm
   
So C or C++? What have you done so far? Any particular problem?
Giving you a code sample does not make a lot of sense -- you can find enough samples on the Web, but it you are stuck with some particular issue, we can help you.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

There are many ways and few ways are mentioned here:
http://www.mrx.net/c/program.html[^]
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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?
   
Comments
Chandrasekharan P 23-Jan-12 6:46am
   
5 for the explanation
Stefan_Lang 23-Jan-12 8:05am
   
Good explanations.

The only thing i disagree with is that I don't consider it so strange that newbies get stuck on these kind of problems: This is often caused by the combination of multiple concepts that the newbie is not yet familiar with. When learning, I've always hated when our professor, to introduce a new programming feature such as for loops, showed us an elaborate example that not only nested the new feature several times over in all kinds of manners, but also added other conepts, some of which we weren't all that familiar with either. The sheer complexity of the total severely distracts from the actual feature being introduced, making it much harder to grasp it.

The same may be true here:
- reading from a file by itself is not difficult.
- Storing a string of unspecified size in a variable isn't either.
- Nor is storing an array or list of values, even when the number of values is not known in advance.

However, if you're not familiar with how to solve at least two of these three aspects, then you may be confounded at how to solve the whole, simply because you don't know where to separate one problem from the other.
Emilio Garavaglia 23-Jan-12 10:03am
   
Good points!
Lakamraju Raghuram 23-Jan-12 21:16pm
   
Well said and by the way if he can understand all the points your raised then he won't be newbie.
My 5
Emilio Garavaglia 24-Jan-12 2:55am
   
That's exactly my point: it is not a problem for newbie. If you know all the required concepts the solution is almost trivial. If you don't, you will never learn them from that problem: there are too many basic concepts contemporary interleaved, that cannot be figured out individually if you never met them individually before. Teachers that toss newbie into that problem are escaping from teaching!
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

If you want C++, the solution is simple, since you can use the container classes from the Standard Template Library (STL). Declare your variable like this:
std::vector<std::string> > lines;

and it will store a variable number of lines of unspecified length. The classes will take care of allocating memory for you.

If you want C only, you need to set up and maintain a data structure of unknown dimensions yourself. As you stated you're just a beginner, I'd rather spare you the details unless the C++ solution is really not an option.

With regard to reading lines from a file, there are functions for both C and C++ to read entire lines of text from a file. Just let us know what language you use (and whether you actually need help with the reading functions)

P.S.:
After reading over solution 2 (and commenting on it), I felt that I should add this:

Your task really has three aspects that you will need to solve:
1. Store an unknown number of values (lines) into an array or list
2. Store a string of unknown length (one line) into a variable
3. Read lines of text from a text file

Try to solve each of these aspects separately, and tell us which of them you still have trouble with, so we can offer more specific help on that.
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 6

thank you everyone..
i got the solution for this problem.

Regards
Jawad
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100