• C

Can I notufy the end of line in a file?

 I have a problem in writing a c program that read from a file and search each line for a special character
the problem is I can not notify the end of line since I read a string each time and store it in an array then
I use a while loop within this array to search for special character ,the condition for this while loop is when I find a character != end of line ?
please I need a help so soon since my boss is expecting this to finish in 1 day?
Who is Participating?
maxm2kConnect With a Mentor Commented:
Try this:
char buff[100];
char ch = '\n';  //i.e. char which indicates eof()

//once you read into a buff
//use for loop
for(i=0; i<=strlen(buff);i++)
How do you read strings from the file? Can you post some code?
The newline character is '\n'
You could use the function fgets(), which can retrieve an entire line at once.
Are the lines in the file terminated with a carriage return or a line feed?
What does the file look like?
Are all of the lines the same length?

What does the program do?
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

fgets will read an entire line, so you know there is going to be a '\n' in buff[strlen(strlen)-1] or not at all (buff was too small.)
although it probably won't make a noticible difference except on larger files, I would remove the strlen call from the for loop.  It is just good programming practice.

int len;
/* some code here */
len = strlen(buff);
for ( i = 0; i < len; i++ )
 /* loop code */
why doing a loop ?  wouldn't be strtok function more appropriate ?
If the line needs to be unmodified (for whatever reason), strchr would be better, but I agree with Oliv.  A loop really isn't needed.
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.
Moderator @ Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.