473,661 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file i/o issue

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
4 1870
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
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
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);
}


"Materialis ed" <ma**********@p rivacy.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2704
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead. > > Can somebody please tell whats wrong?? > > This is the code : : #include<fstream.h> #include<conio.h>
3
1918
by: gmtonyhoyt | last post by:
Okay, this one's a tough one for me to explain so this might take a few e-mails to get the idea across. Here's what I got though. I have this application running on a Sun/Solaris machine, written in C, using the Sun Forte Developer 7 C 5.4 Compiler released a few years ago. Not exactly old but, you get the picture. Now, most if not all the file handles on this machine are most likely being used up by a secondary process and/or...
5
2864
by: Jay Ge | last post by:
I searched this issue in internet, but they still cannot solve my issue, so your help/suggestion will be preciated. this page is placed on serverA, and it will touch files on serverB.(but it seems this is not double hop issue.) I use impersonation <identity impersonate="true" userName="domain\username" password="passwd" /> and <authentication mode="Windows"/>. the account "domain\username" is the admin of serverA (I have grant it admin...
7
6055
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
5
4463
by: Al | last post by:
Hi all We have created a xml file that imports a single project using the Import element. This project compiles to a class library, but has references to two other projects that are also class libraries. We are having a path reference issue for the depending projects; if the build is started in another directory than that of the imported project. (It's a simple cannot find project xyz.csproj, build fails.) If we place the xml file in...
3
4922
by: J055 | last post by:
Hi How do I tell the user he has tried to upload a file which is too big... 1. when the httpRuntime.maxRequestLength has been exceeded and 2. when the uploaded file is under then httpRuntime.maxRequestLength For point 1. it would be good to at least display a nice error page. IE seems to just display a blank page.
6
1717
by: PipedreamerGrey | last post by:
I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file (index.rtf). Now I'm adapting the script to convert all the text files into individual html files. What I can't figure out is how to trigger a change in the background value for each folder change (not each file), so that text is color-coded by...
5
3048
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a .Net web service that converts the MODCA files to tif or png file format. This web service runs on a 2003 server. This web service first copies the MODCA file to be converted to a temporary conversion folder. Then it converts it to the desired file format and copies the file to the output folder specified. Then it deletes the converted file from the temporary conversion folder. Then it attempts to delete the copied input file...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.