![]() |
How to read a line of from a file
Hi,
I have a text file (Linux) that stores one sentence per line (no periods & 161 characters max including newline). I'd like to read a line from the file and store it in an array. The array will then be used to search for specific words. I need to do this until the end-of-file is reached. The following is the code I'm using but it doesn't work properly. I assume it's because fgets() reads until either EOF is reached or until the number of characters specified is read. I haven't worked with files too often so I'm not very good at it but I'm trying to learn. Any suggestions is greatly appreciated. :
int somefunction(char *name) |
It might help if you specified what way your code isn't working properly, and actually gave a small but complete code example that really illustrates your problem.
But some things to check; 1) If the maximum length of a line is 161, including newline, the length of the buffer (temp in your example) needs to be 162, to allow for a trailing zero byte. 2) Your code refers to a number of things that are not defined or declared in the snippet you've given. As is, this snippet would not even compile. For example, what is kbsize? Also, the line "kb[current++].init(temp);" refers to things (an array (?) called kb, a variable called current, and something called init) that are not defined in the code snippet you've given. If this line (which looks suspiciously like it might be a call to a C++ member function) is messing up, you will need to look at what this line is doing. |
I think aznluvsmc didn't include all the source. I see only 1 function, but where is your main function??? Please include all source if you have it.
|
This is actually a C++ program using member functions and 2 classes where one class is a data member of another class but the code is still C syntax for the most part. No cin, cout or fstreams. I can move it to the C++ forums but since it's C syntax I thought it's better to go into the C forum.
I could also post the code which is fairly lengthy but it's irrelevant to the problem. I tried changing the buffer to 162 but that didn't seem to work either. I think the better question to ask is, does fgets() stop reading when it encounters the newline character or does it read up to the buffer size or EOF depending on which comes first? If you do need the entire source code, I can post it. Let me know for sure. |
I would consider the code you just posted C, although it's part of a C++ program.
:
int somefunction(char *name)Is this what you were looking for? |
Actually, after looking over the code again, I realized it does work. The problem was that when I was seeing the output for puts(temp) I saw 5 lines one after the other and I thought that that was the content of temp after one loop iteration but I realized I didn't put any separators to make it clear how many iterations it was doing.
The loop does seem to read upto and including the newline, assign to temp, call my member function and then read the next line continuing until EOF or until current < kbsize is false. Thanks for you help anyways. Tempest, that could also work so I'll keep that in mind as one technique for working with files. |
Quote:
|
:
int somefunction(char *name) |
Quote:
Unfortunately, sometimes we just need to ask questions. I've read text books on C and C++ but they can only tell you so much and somtimes it isn't clear. |
A quick google with the keywords "man 3 fgets":
Quote:
|
| All times are GMT -5. The time now is 8:39 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC