Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 21st, 2005, 11:11 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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;
}
aznluvsmc is offline   Reply With Quote
Old Oct 22nd, 2005, 3:02 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Oct 22nd, 2005, 4:49 AM   #3
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
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.
ivan is offline   Reply With Quote
Old Oct 22nd, 2005, 9:21 AM   #4
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Oct 22nd, 2005, 9:33 AM   #5
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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?
__________________

tempest is offline   Reply With Quote
Old Oct 22nd, 2005, 9:43 AM   #6
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Oct 22nd, 2005, 10:05 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
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?
This is one of the very first questions that a look at the documentation for "fgets" will answer. Did you even try that?
__________________
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
DaWei is offline   Reply With Quote
Old Oct 22nd, 2005, 11:14 AM   #8
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
int somefunction(char *name)
{
        char temp[1024], tmp
	int i=0, added=0;
You're missing a semi-colon too.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Oct 22nd, 2005, 3:13 PM   #9
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Quote:
Originally Posted by DaWei
This is one of the very first questions that a look at the documentation for "fgets" will answer. Did you even try that?
Unfortunately, my C Primer Plus book doesn't discuss this very much. It states that fgets() reads through the newline. I assume that means, it doesn't stop until EOF or the number of characters sepcified is reached but if that's so, my code shows otherwise.

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.
aznluvsmc is offline   Reply With Quote
Old Oct 22nd, 2005, 3:51 PM   #10
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
A quick google with the keywords "man 3 fgets":

Quote:
Originally Posted by Some site on the internet
The fgets() function reads at most one less than the number of characters
specified by size from the given stream and stores them in the string
str. Reading stops when a newline character is found, at end-of-file or
error. The newline, if any, is retained. If any characters are read and
there is no error, a `\0' character is appended to end the string.

The gets() function is equivalent to fgets() with an infinite size and a
stream of stdin, except that the newline character (if any) is not stored
in the string. It is the caller's responsibility to ensure that the
input line, if any, is sufficiently short to fit in the string.
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC