Link to home
Start Free TrialLog in
Avatar of higijj
higijj

asked on

reading files

Hi!

Here is my problem:

I got a text file which contains other text file's name (one by line)
Ex:
file1.cfg
file2.cfg
file3.cfg


When I loop through the file with fget to read each filename, and then pass this filename to fopen, it give me a seg fault because of the newline char fget reads. So I first though to use fscanf instead of fget and worked well until I realize it wouldn't read filename containing space and also, it doesn't read 'til the eof like fget did.

So my question is how can I remove the new line char read by fget?

I thought I could use strncpy(dest, source, strlen(source) - 1) but it didn't work :(

Thanks!
HiGiJJ!
Avatar of akshayxx
akshayxx
Flag of United States of America image

lets say  u have got the filename in ur variable
char fname[MAXLEN];
and u r sure there is one newline in this . that is at the end .. then do this

quick way
fname[strlen(fname)-1]='\0';
 // this will not work if u have more than one newline in the string fname

here is another dirty but sure way
char *idx;
idx=strchr(fname,'\n');
*idx='\0';




btw show ur program and i'll suggest the exact change required to make it work..
ASKER CERTIFIED SOLUTION
Avatar of akshayxx
akshayxx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of higijj
higijj

ASKER

Thanks a lot!!

the files are too complicated (not to understand, but spread over many files) to paste it here. but I gave it a try and worked perfectly!

thanks!
nice to know it worked .. good luck with rest of ur work