Archived

This topic is now archived and is closed to further replies.

C File i/o help

This topic is 5587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a program that stores a telephone directory of sorts in to a file. I have a problem. I have compiled my program and it creates the file that stores the telephone directory fine. No problem there. I have checked the contents of the file - its writing all correctly. What I want to be able to do is to start up the program, load in the directory and read the contents using a small search engine in my program. The problem is the program can't open the file and I don't know why. Here is the code for both the save and the load functions. The save is executing fine as I said but I can't even load the file. Can't someone tell me why? void savedirectory() { FILE *directory; if ((directory=fopen("phone","w"))==NULL) { printf("The file cannot be created. "); return 0; } for (int i=0; i < count; i++) { fprintf(directory, "%s\n%s\n",namearr, numarr[i]); } fclose(directory); } The load function is here: - int loaddirectory() { FILE *directory; if ((directory=fopen("phone","r"))==NULL) { printf("\nError opening file."); return 0; } int i=0; while (!feof(directory)) { fgets(namearr[i],20,directory); fgets(numarr[i++],20,directory); } fclose(directory); } HELP! [edited by - SteveBe on August 29, 2002 10:54:12 AM]

Share this post


Link to post
Share on other sites
Silly question, but is the file read only?
Do you check to make sure you close the file or you do not have it open in Notepad or some other editor while trying to open it?

-Triston

All the world''s a stage...and I seem to fall off quite a bit.

Share this post


Link to post
Share on other sites
I don''t know why but my code is inaccurately represented above in the save function. I have tried editing it to no avail. Its only a couple of small things like the subscripts of the array are missing but they are there believe me. The problem is in the load function.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The only real problem I see is that "phone" doesn''t have some sort of extension. And depending on which compiler your using it the C File IO might determine that file to be a "directory" instead of a file thus causing problems. Other than that it could be a path issue. Look to see where that file''s being saved, if it''s being saved.

Share this post


Link to post
Share on other sites
Triston - if it was read only then I would be able to read from the file wouldn't I?

Thats my problem. I can write the file but not open it to read. The file isn't open in Notepad or anything - I've checked that already. I'm completely stuck.

Anonymous poster - I have tried using an extension. That doesn't work. As for the path, the compiler has saved the telephone directory into the directory occupied by the compiler. If it utilises this search path for a write operation, will it not use the same path for a read operation?

[edited by - SteveBe on August 29, 2002 11:00:22 AM]

Share this post


Link to post
Share on other sites
Melraidin - I might be misunderstanding your question - but nothing loads. The file doesn''t open period. When I go through the code step by step in the debugger (I''m using Dev C++ by the way) it comes up with error SIGSEV fault (I think thats it) when it reaches the line to load the file.

Can someone tell me how to make a code window here so I can put the code for the whole program in.

Share this post


Link to post
Share on other sites
Use [] brackets with the word "source" inside (no quotes) to open and the same with a /source to close


  
void savedirectory()
{

FILE *directory;

if ((directory=fopen("phone","w"))==NULL)
{
printf("The file cannot be created. ");

// returning an int on a void functions?

return 0;
}

// i guess count comes from the same place as namearr and numarr?

for (int i=0; i < count; i++)
{
fprintf(directory, "%s\n%s\n",namearr[i], numarr[i]);
}

fclose(directory);

}


The load function is here: -


int loaddirectory()
{
FILE *directory;

if ((directory=fopen("phone","r"))==NULL)
{
printf("\nError opening file.");
return 0;
}


int i=0;

while (!feof(directory))
{
fgets(namearr[i],20,directory);
fgets(numarr[i++],20,directory);
}

fclose(directory);

// don''t forget to return something here too

return 0;
}

HELP!


Share this post


Link to post
Share on other sites

    
#include <stdio.h>
#include <string.h>


char namearr[100][20]; //these are global right now until I can

char numarr[100][20]; //work multidim. arrays with functions.

int count=0; //tracks no. of elements in arrays.

//will put in main and alter by pointer this is early stages.



int printmenu(void);
void nameentry(void);
void findnumber(void);
int savedirectory(void);
int loaddirectory(void);


int main(void)
{
int userchoice;

do
{
switch(userchoice=printmenu())
{
case 1:
nameentry();
break;
case 2:
findnumber();
break;
case 3:
savedirectory();
break;
case 4:
loaddirectory();
break;
case 5:
return 0;
}
}while (userchoice!=5);

return 0;
}

int printmenu(void)
{
int userchoice;

printf("Telephone Directory Program: ");
printf("\n\n1. Enter names and numbers");
printf("\n2. Find numbers");
printf("\n3. Save directory");
printf("\n4. Load directory");
printf("\n5. Quit");
printf("\n\nPlease enter your choice now: ");
scanf("%d%*c",&userchoice);

return userchoice;
}


void nameentry(void)
{
do
{
printf("\n\nPlease enter the name. q to quit : ");
gets(namearr[count]);

if (!strcmp(namearr[count],"q"))
break;

printf("\nPlease enter %s's number: ",namearr[count]);
gets(numarr[count++]);

}while (strcmp(namearr[count],"q"));
}

void findnumber(void)
{
char name[20];
int found=0;

printf("\n\nPlease enter the name of the person: ");
gets(name);

for (int i=0; i<count; i++)
{
if (!strcmp(namearr[i],name))
{
printf("\nThis person has been found.\n\nTheir number is %s",numarr[i]);
found=1;
break;
}
}
if (!found)
{
printf("\n\nThe number of %s was not found in the directory.",name);
}
printf("\n\n\n");
}



int savedirectory(void)
{

FILE *directory;

if ((directory=fopen("phone","w"))==NULL)
{
printf("The file cannot be created. ");
return 0;
}

for (int i=0; i<count; i++)
{
fprintf(directory, "%s\n%s",namearr[i], numarr[i]);
}

fclose(directory);

}


int loaddirectory(void)
{
FILE *directory;
int i=0;

if ((directory=fopen("phone","r"))==NULL)
{
printf("\nError opening file.");
return 0;
}


while (!feof(directory))
{
fgets(namearr[i],20,directory);
fgets(numarr[i++],20,directory);
}

fclose(directory);
}




[edited by - SteveBe on August 29, 2002 12:19:57 PM]

Share this post


Link to post
Share on other sites
Post more information about the error you are getting when opening the file.
I''ve never heard of a SIGSEV!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Share this post


Link to post
Share on other sites
The code compiles and works here; I can save and load files just fine (the search function is faulty, but that wasn''t the issue here). Something else is causing your problem.

When you are stepping through your code, do you step into the loaddirectory() function? It seems strange that fopen() should result in SIGSEGV, but the gets() calls could very easily do that given bad input.

Share this post


Link to post
Share on other sites
Spock - please tell me in what way is the search function faulty. I want to know my mistakes and learn from them!

And yes, I am stepping into the code. And it comes up with sigsegv fault on the fopen() command.


As for gets() I know there is no error checking (was going to add that when I had the bare bones built) but I'm making sure that my input is valid.


[edited by - SteveBe on August 30, 2002 7:58:13 AM]

Share this post


Link to post
Share on other sites
I didn''t actually debug the program, I merely experienced that the search function wouldn''t work after loading. It''s probably because you are mixing reads using fgets() and gets() - fgets() will include the newline in the string while gets() will replace the newline with a null character.

fopen() shouldn''t SIGSEGV like that. Does it crash when running a compiled executable outside the development environment and using valid input?

Share this post


Link to post
Share on other sites
I tested your code in MSVC, no problem in opening the file, but you have another bug in your loadlibrary function. As you read the values in the file using the while loop, you don''t increment the value of your global variable "count". So when you select "find numbers" although you have loaded the file correctly, you have count == 0; and you return false..

hope that helps..

Share this post


Link to post
Share on other sites