?
Solved

reading files

Posted on 2003-03-10
4
Medium Priority
?
260 Views
Last Modified: 2012-05-04
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!
0
Comment
Question by:higijj
[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
  • 3
4 Comments
 
LVL 8

Expert Comment

by:akshayxx
ID: 8107741
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..
0
 
LVL 8

Accepted Solution

by:
akshayxx earned 200 total points
ID: 8108096
>>So my question is how can I remove the new line char read by fget?
this ensures u'll have single new line for each fgets
only if  that has buffer size large enoough , so that it reads the full line..

so once u ensure that, then
fname[strlen(fname)-1]='\0';
is enough .. and before opening file make a check that the fname has something in it
if(strlen(fname)>0){
open the file and check if u were success ful to open the file
}

0
 
LVL 2

Author Comment

by:higijj
ID: 8108325
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!
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8108599
nice to know it worked .. good luck with rest of ur work
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

765 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