473,387 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

file open error

Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!
void LICENSEinfo::find()
{
char check[10],vendor[20],date[12];
char uw1[20],uw2[20],mystring[200];
int lic_num;
char filestring[20];

struct dirent *dp, *subdp;
DIR *dir, *subdir;

FILE *FILE_list;
FILE *FILE_lic;

FILE_list = fopen("/tmp/list","w");

// Make "/parent" as the present working directory
chdir ("/parent");

dir=opendir(".");
while ((dp=readdir(dir))!=NULL)
{
if(!strcmp(dp->d_name,".")||!strcmp(dp->d_name,".."))
continue;

subdir=opendir(dp->d_name);
while ((subdp=readdir(subdir))!=NULL)
{
if(!strcmp(subdp->d_name,".")||!strcmp(subdp->d_name,".."))
continue;

if(strstr(subdp->d_name,"_lic")!=0)
{

// read the entire file
strcpy(filestring,subdp->d_name);
FILE_lic = fopen(filestring,"r");
// problem: FILE_lic is reset to 0x0 here.
while (feof(FILE_lic)==0)
{
fgets(mystring,200,FILE_lic);
if((strstr(mystring,"INCREMENT"))||(strstr(mystrin g,"FEATURE")))
{
sscanf(mystring,"%s%s%s%s%s%d",check,uw1,vendor,uw 2,date,&lic_num);
fprintf (FILE_list,"%s \t %s \t %d \n",vendor,date,lic_num);
}
}

}

}
}

}


Thanks in advance
Jul 22 '05 #1
2 5434
Hi,

"Abhijit" <ab*********@yahoo.com> wrote in message
news:45*************************@posting.google.co m...
Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!
void LICENSEinfo::find()
{
char check[10],vendor[20],date[12];
char uw1[20],uw2[20],mystring[200];
int lic_num;
char filestring[20];

struct dirent *dp, *subdp;
DIR *dir, *subdir;

FILE *FILE_list;
FILE *FILE_lic;

FILE_list = fopen("/tmp/list","w");

// Make "/parent" as the present working directory
chdir ("/parent");

dir=opendir(".");
while ((dp=readdir(dir))!=NULL)
{
if(!strcmp(dp->d_name,".")||!strcmp(dp->d_name,".."))
continue;

subdir=opendir(dp->d_name);
while ((subdp=readdir(subdir))!=NULL)
{
if(!strcmp(subdp->d_name,".")||!strcmp(subdp->d_name,".."))
continue;

if(strstr(subdp->d_name,"_lic")!=0)
{

// read the entire file
strcpy(filestring,subdp->d_name);
FILE_lic = fopen(filestring,"r");
In the current directory there isn't a file with the contents of
subdp->d_name. Hence fopen can't find the file and returns 0.

You should first change with chdir to the child directory you are reading
from. Though you opened the subdir for reading with readdir, your working
directory is still set to the parent of the subdir.

Regards, Ron AF Greve.
// problem: FILE_lic is reset to 0x0 here.
while (feof(FILE_lic)==0)
{
fgets(mystring,200,FILE_lic);
if((strstr(mystring,"INCREMENT"))||(strstr(mystrin g,"FEATURE")))
{
sscanf(mystring,"%s%s%s%s%s%d",check,uw1,vendor,uw 2,date,&lic_num);
fprintf (FILE_list,"%s \t %s \t %d \n",vendor,date,lic_num);
}
}

}

}
}

}


Thanks in advance

Jul 22 '05 #2
Abhijit wrote:
Hello everybody,

I am getting a "segmentation fault" error when i compile the following
piece of code. The program runs fine till it encounters the line of
code saying" FILE_lic=fopen.........". And after that, it terminates
saying segmentation fault. I had included the appropriate header files
also! I couldn't makeout where the problem exists in the program. Can
anyone solve this mistery!
FILE_lic = fopen(filestring,"r");
// problem: FILE_lic is reset to 0x0 here.

Do you understand what it means when the return value from fopen() is
0? If not, why not? What book are you using?

Do you understand that FILE_lic is a pointer? Do you understand what
happens if you attempt to use a null pointer? If not, why not?

Brian Rodenborn
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
6
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004...
2
by: Lee Swanson | last post by:
Hi, I have a fairly large visual studio.net solution made up of about 8 projects. I needed to update a usercontrol in one of these projects, but when I tried to open it I got the error 'The...
7
by: Mark | last post by:
Hello, I have researched and tried every thing I have found on the web, in groups and MS KB articles. Here is what I have. I have a Windows 2000 Domain Controller all service packs and...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
0
by: hidara | last post by:
I have an excel file that I need to check if its in use by another before i open it. I have found various forms of the follwing code but each one give me the same errors at the following 2 lines...
3
by: ArmageddonAsh | last post by:
I'm trying to make an application that will allow the user to enter data into a flexgrid (that's done) and then save the data from that flexgrid into a CSV file but even though the file is made none...
9
by: tshad | last post by:
I am trying to get access to a file that may still being written because the file is so large (7-10MB). I get an error: The process cannot access the file 'c:\TestDocs\XMLFiles\492172.XML'...
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.