hello there,
Im trying to get one of the functions for my code to work which reads in two text files line by line and compares each line and so far i havent been able to do so, i am trying to read the first line of a text file only and store that in a char array, i have only been able to find how to store an entire file or contents of a directory into an array not the first line, i have tried using fgets but with no avail, any sujestions or things to try would be great
thanks in advance

fgets(charArrayName, sizeof(charArrayName), filePointerName) is what you want, clearly.

It pulls in one line of text at a time, including the newline at the end of the line (space permitting), and adds the required end of string char, for you. That allows the whole line of text to be used as one big string, with any of C's string handling functions (to use them, include <string.h>).

So get started! Make sure your char array is comfortably bigger than the rows of text you are getting.

@Nonoob:

You posted a workable snippet of code before the OP posted any code -- you're going straight to hell! :p :p :p

04-01-2011

nonoob

Yeah.. I should have stopped at emphasizing open mode not being "binary". But strangely, the OP's attempt to use fgets failed him. So I did not know where else he may have gone wrong. I anticipated most issues such as buffer overrun, end-of-line terminator, failure to open file, error handling, etc. I just saved him and us another 20 posts.

But you're right. It's more effective learning when one makes one’s own mistakes.

04-01-2011

Adak

@Nonoob, that was just a joke - don't take it seriously. You did exactly the right thing, of course.

Here you should open your input files using "r" not "a+" unless you want the file pointer at the end of the file...

Code:

//-delete line 1 of each text file
//-therefore old second line should become new first line

Don't try to delete it from your input files. That is not as simple as you may think. Simply ignore the first line when reading it.

A few other issues...
You are not checking the return values of your fopen() calls. What are you going to do if one of the files doesn't open?

You are also not checking the returns from your fgets(). What happens if one hits end of file before the other and returns 0?

You never want to clear a string buffer to spaces. Either set it to 0s or simply use sll1[0] = 0 ... which is adequate to tell the string library functions it's empty.

On a more general level... do try to pick more descriptive names for your variables ... sll1 is pretty cryptic, use descriptive names like ServerString or SrvDat ....

04-01-2011

Ciaran

In response the three files are created by another function in the program and checked there, this just opens them, I tried to just ignore the first line but as the compare moves down it is comparing each line one and if sll1 (server list line 1) doesn't match cll1 (client list line 1) then it gets added to a copy list and has to move on to the next entry in the server list to compare with the copy list, hence why i need to be able to delete a line, i read somewhere that it is easier just to create a new file that excludes the first line or something.
but im most welcoming of any ideas or suggestions to make it work better, or more simply

04-01-2011

CommonTater

Opening a file is NEVER guaranteed. What happens if there's a problem writing the file and it never gets created... When doing IO work, ALWAYS test for success.

Ignoring a line from a file is easy... call fgets() to read the first line of each file. Then simply start your loop to compare the rest... It's no big issue, just one extra step.