By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

file i/o issue

P: n/a
Hi everyone,

I have the following program

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp1, *fp2;
char s[80];

if((fp1=fopen("/usr/local/www/cgi-bin/data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}
while(!feof(fp1))
{
fscanf(fp1, "%s", s);
fprintf(fp2, "%s\n", s);

}
fclose(fp1);
fclose(fp2);
return 0;
}

However it gives me a segmentation fault.
The permissions on the file(s) are not the issue, but if I change the
code to read
if((fp1=fopen("data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("output.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}

The code works fine.
Does anyone know the issue here?

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Materialised wrote:
Hi everyone,

I have the following program

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp1, *fp2;
char s[80];

if((fp1=fopen("/usr/local/www/cgi-bin/data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
Non-standard return value. Use EXIT_FAILURE instead.
}
if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}
while(!feof(fp1))
See: http://www.eskimo.com/~scs/C-faq/q12.2.html
{
fscanf(fp1, "%s", s);
Bad idea. Though it's written about scanf(), it applies to fscanf()
as well...see http://www.eskimo.com/~scs/C-faq/q12.20.html
fprintf(fp2, "%s\n", s);

}
fclose(fp1);
fclose(fp2);
return 0;
}

However it gives me a segmentation fault.
The permissions on the file(s) are not the issue, but if I change the
code to read
if((fp1=fopen("data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("output.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}

The code works fine.
Does anyone know the issue here?

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Nov 13 '05 #2

P: n/a
Mac
On Sun, 23 Nov 2003 03:43:25 +0000, Materialised wrote:
Hi everyone,

I have the following program

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp1, *fp2;
char s[80];

if((fp1=fopen("/usr/local/www/cgi-bin/data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}
while(!feof(fp1))
{
fscanf(fp1, "%s", s);
fprintf(fp2, "%s\n", s);

}
fclose(fp1);
fclose(fp2);
return 0;
}

However it gives me a segmentation fault.
The permissions on the file(s) are not the issue, but if I change the
code to read
if((fp1=fopen("data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("output.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}

The code works fine.
Does anyone know the issue here?


Artie gave you some good pointers. The most likely problem is that you are
reading more than 80 characters into s. This doesn't happen when you read
data.txt, because data.txt in the current working directory isn't the same
as data.txt in the other directory. More specifically, data.txt in the
other directory contains a contiguous series of non white space characters
80 or more characters long. Even if the files are identical, it could be
that it causes a problem in one place and not in the other, since it is
formally undefined behavior.

One way to fix the seg fault problem (but not necessarily the program
logic) is to restrict the size of your read to the size of your buffer.
Like this:

s[79]='\0';
...
fscanf(fp1, "%79s", s);

Using magic numbers like this is a maintenance nightmare, of course. So
you may want to explore other options. (you could sprintf a format string
prior to calling fscanf)

You could also ditch fscanf() and use fgets instead, but the two don't do
exactly the same thing.

mac
--

Nov 13 '05 #3

P: n/a
a seperate issue but u need 2 close fp1 in your fp2 error code.

if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
*** fclose(fp1);
exit(1);
}


"Materialised" <ma**********@privacy.net> wrote in message
news:bp*************@ID-204621.news.uni-berlin.de... Hi everyone,

I have the following program

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp1, *fp2;
char s[80];

if((fp1=fopen("/usr/local/www/cgi-bin/data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}
while(!feof(fp1))
{
fscanf(fp1, "%s", s);
fprintf(fp2, "%s\n", s);

}
fclose(fp1);
fclose(fp2);
return 0;
}

However it gives me a segmentation fault.
The permissions on the file(s) are not the issue, but if I change the
code to read
if((fp1=fopen("data.txt", "r"))==NULL)
{
printf("Cannot open file 1\n");
exit(1);
}
if((fp2=fopen("output.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");
exit(1);
}

The code works fine.
Does anyone know the issue here?

Nov 13 '05 #4

P: n/a
On Sun, 23 Nov 2003 18:21:44 -0500, Ron Shaw wrote:
a seperate issue but u need 2 close fp1 in your fp2 error code.

if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)
{
printf("Cannot open file 2\n");


*** fclose(fp1);
exit(1);
}


That's not strictly necessary: exit() will close all open files for you. I
agree that it's probably good form, though.

Josh
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.