file i/o issue

M

Materialised

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?
 
A

Artie Gold

Materialised said:
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
 
M

Mac

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
--
 
R

Ron Shaw

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 said:
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?
 
J

Josh Sebastian

a seperate issue but u need 2 close fp1 in your fp2 error code.

if((fp2=fopen("/usr/home/mick/data.txt", "w"))==NULL)

*** fclose(fp1);

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

Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top