![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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)
{
char temp[161];
int added = 0;
FILE *fin;
if ((fin = fopen(name, "r")) == NULL)
{
printf("Error opening file \"%s\" for reading\n", name);
exit(1);
}
while (current < kbsize && fgets(temp, 161, fin))
{
puts(temp); /* just checking to see what temp contains */
kb[current++].init(temp);
added++;
}
if (fclose(fin) != 0)
printf("Error closing file \"%s\"\n", name);
return added;
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
I would consider the code you just posted C, although it's part of a C++ program.
int somefunction(char *name)
{
char temp[1024], tmp
int i=0, added=0;
FILE *fin;
if ((fin = fopen(name, "r")) == NULL)
{
printf("Error opening file \"%s\" for reading\n", name);
exit(1);
}
while(!feof(fin)) {
while((tmp = fgetc(fin)) != '\n' && tmp != EOF)
temp[i++] = tmp;
temp[i] = '\0';
i=0;
kb[current++].init(temp);
added++;
}
if (fclose(fin) != 0)
printf("Error closing file \"%s\"\n", name);
return added;
}Is this what you were looking for?
__________________
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#8 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
int somefunction(char *name)
{
char temp[1024], tmp
int i=0, added=0;
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
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. |
|
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
A quick google with the keywords "man 3 fgets":
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|