Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I notufy the end of line in a file?

Posted on 1999-09-06
8
Medium Priority
?
205 Views
Last Modified: 2010-04-15
Hi
 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?
0
Comment
Question by:NHA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2025853
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.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 2025945
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?
0
 

Accepted Solution

by:
maxm2k earned 200 total points
ID: 2026128
Try this:
//declarations
char buff[100];
char ch = '\n';  //i.e. char which indicates eof()
..
..
..

fgets(buff,sizeof(buff),fp);
//once you read into a buff
//use for loop
for(i=0; i<=strlen(buff);i++)
{
   if(ch==buff[i])
      break;
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2026757
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.)
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2027559
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 */
 }
0
 

Expert Comment

by:Oliv
ID: 2029731
why doing a loop ?  wouldn't be strtok function more appropriate ?
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2029962
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.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6820390
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.
Moondancer
Moderator @ Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question