#### Archived

This topic is now archived and is closed to further replies.

# Reading text from a file, fgets

This topic is 6624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m having a problem reading text from a file. The code I have is as follows.  char *text; char line[255]; FILE *file; file = fopen("text.txt, "r"); if (file == null) { ... } fgets(line, 255, file); text = malloc(strlen(line)); strcpy(text, line); printf("line reads: %s", line); printf("text reads: %s", text); fclose(file);  This code isn''t exactly what I''m doing, I did this for brevity, but either way line and text both read as NULL. The file I''m reading from does contain text, the first line is text, yet I can''t read it for some reason. Any help would be greatly appreciated. ------------ - outRider -

##### Share on other sites
1) You need to terminate the string text.txt with a ''"'' it overlaps into r.

2) NULL should be capitalized.

hmm, thats all i immeadiatly see w/o error codes, the file, etc.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

##### Share on other sites
Actually, both those mistakes are mistakes in the post, not my source, sorry about that. NULL is capitalized, and the "text.txt" is a parameter in a func which stores it in a char* which is then passed to fopen();

The text file is a regular text file I created in Notepad. It's like this basically...

--------------------start of file
some text

some more text
--------------------end of file

If you opened it thats what you would see, text at the very first line, which should be read, but isn't...

Thanks for the help anyway, if anyone has any other ideas please let me know, I'm trying my best to learn C and I appreciate this forum's assistance.

------------
- outRider -

Edited by - outRider on September 1, 2000 7:13:17 PM

##### Share on other sites
Are you opening the file in text mode? I dont use fgets() so I cant be of much help... you should use ifstream::read() anyway =) Wait, one thing you might be doing is overflowing line. You declare it as char[255], and are reading 255 chars into it... but what about the null , sorry NULL char which is appended by strcpy()? It is probably overwriting your text pointer. Hope that helps!

##### Share on other sites
Orpheum, get off your high horse, C++ is not the be-all and end-all.

but it starts as zero so his array is really 256 chars long. Although, its worth a try.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Edited by - immaGNUman on September 1, 2000 8:35:19 PM

##### Share on other sites
I''d have to see the original code. And when you do a strcpy, make sure you malloc''ed one more for the NUL character (the \0).

##### Share on other sites
CHeck the output of fgets, if it''s NULL there was a read error, also you can put:

text = fgets( line, 255, file );
if( text == NULL )
// fgets error
else
// text == line.

Try that, it should work as the rest of your code looks like it would work correctly.

##### Share on other sites
Here's the original code I had. Maybe theres a mistake somewhere that didn't show up when I paraphrased it earlier.

//globalstypedef struct{ float x, y, z;} VERTEX;typedef struct{ VERTEX Vertex[3];} TRIANGLE;typedef struct{ short PolygonCount; TRIANGLE *Mesh;} OBJECT;OBJECT Model;unsigned char LoadObject(const char *Filename, OBJECT *Object){ FILE *File; char Line[256]; char *Ret; short TriangleLoop, VertexLoop; File = fopen(Filename, "r"); if (File == NULL) { MessageBox(0, "Unable to load required file.", "Error", MB_OK | MB_ICONERROR); return FALSE; } do { Ret = fgets(Line, 255, File); } while ((Ret != NULL) && (Line[0] == '/') || (Line[0] == '\n')); sscanf(Line, "Polygon Count: %d\n", &Object->PolygonCount); Object->Mesh = malloc(Object->PolygonCount); for (TriangleLoop = 0; TriangleLoop < Object->PolygonCount; TriangleLoop++) { for (VertexLoop = 0; VertexLoop < 3; VertexLoop++) { do { Ret = fgets(Line, 255, File); } while ((Ret != NULL) && (Line[0] == '/') || (Line[0] == '\n')); sscanf(Line, "%f, %f, %f", &Object->Mesh[TriangleLoop].Vertex[VertexLoop].x, &Object->Mesh[TriangleLoop].Vertex[VertexLoop].y, &Object->Mesh[TriangleLoop].Vertex[VertexLoop].z); } } fclose(File); return TRUE;}WinMain(...){ if(!LoadObject("text.txt", &Model) { ...exit app... }}

Basically what it's doing is reading the first line, which is Polygon Count: , creating enough storage for that amount, and reading the rest of file, into that storage but for some reason fgets is not reading into line at all. If I do

then the output would be "Line reads: (NULL)"

For some reason the program starts, and I load the file, then initialize OpenGL, but I have to push enter or the program won't get past LoadObject, and when I do push enter LoadObject returns false so the app exits.

I don't have a debugger yet so I can't step through to find exactly what's going on, but I think the above is accurate, since currently there's only one spot where the function can return false, even though it doesn't display the popup message.

And I'm not interested in CPP file i/o funcs, I'd rather do it in C.

------------
- outRider -

Edited by - outRider on September 2, 2000 8:32:50 AM

Edited by - outRider on September 2, 2000 11:14:22 AM

##### Share on other sites
Actually, I figured out what the problem was, File is equal to NULL after fopen() so the file can''t be opened. The message box does pop up, but since its a top level popup window it doesn''t show up, thats why you have to push enter, to get rid of the message box, and then the app exits.

Do these i/o funcs even work in Win32? Or are they Dos funcs that can''t be used, so therefore I would have to use their Win32 API equivalents?

------------
- outRider -

##### Share on other sites
Try "C:/foo/bar/baz" or if the text file is in the same exact directory as the compile .exe "/text.txt".

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632988
• Total Posts
3009737
• ### Who's Online (See full list)

There are no registered users currently online

×