Advertisement

Fscanf Help!

Started by July 11, 2000 01:30 PM
10 comments, last by CoiN 24 years, 5 months ago
Below is the code im using to store and read data to and from a text file, it saves it fine to the disk but, when reading it back it only reads upto the first space each time (I know fscanf does this ). I was just wondering whether theres an easier way to read string upto the Newline character that reading it into the array char by char. I know its a bit long but....
    

// LOAD DIRECTORY TO DISK

if (selection == 4)
{
fflush(stdin);
printf (" Please enter name and path of the file you wish to open ");
printf (" Name and Path: ");
gets(filename);
// Open File

if ((fstorage = fopen(filename, "r")) == NULL)
{
printf (" Error Creating File... ");
} 

// If file is open get data

if (fstorage != NULL)
{

for(count=1;count<100;count++)
{
printf (" NAH! %d ", count);

fscanf (fstorage, "%100s", name[count]);
fscanf (fstorage, "%100s", telnum[count]);
}
}

}

Thats the code i use for getting the data from the file, and below is the source i use for putting it in the file:

// SAVE DIRECTORY TO DISK

if(selection == 3)
{
fflush(stdin);
printf (" Please enter name and path of the file you wish to save ");
printf (" If no path is entered the file will be placed in a default dir ");
printf (" Name and Path: ");
gets(filename);

// Open File

if ((fstorage = fopen(filename, "w")) == NULL)
{
printf (" Error Creating File... ");
} 

// If file is open store data

if (fstorage != NULL)
{
for(count=1;count<100;count++)
{
fputs(name[count],fstorage);
fprintf(fstorage, " ");
fputs(telnum[count],fstorage);
fprintf(fstorage, " ");
}
}

}

    
Thanks in Adnvace.....
Trying using fgets() - that will return a single line of text up to and including the carriage return/newline.

// CHRIS
// CHRIS [win32mfc]
Advertisement
Thanks for the reply....

The problem is that i really dont want that newline character read in because, next time i try and save it to a file, it will add an extra newline and then cause problems when i try too load it, the text file would look like this:

            name<space>number<space>name <space>number<space>name<space>number<space>When it should be like this:namenumbernamenumbernamenumber    


If youve got any other solutions as to how i could save it to the file, lemme know

Thanks in advance...

Edited by - CoiN on July 11, 2000 6:54:43 PM

Edited by - CoiN on July 11, 2000 6:55:38 PM

Edited by - CoiN on July 11, 2000 6:57:12 PM
Here is an idea.
You can use fputc and fgetc with a terminating flag such as 0

and just do something like
len=strlen(string);
for (charptr=0; charptr

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

Here is an idea.
You can use fputc and fgetc with a terminating flag such as 0

and just do something like
len=strlen(string);
for (charptr=0; charptr

Firstly, don''t use gets, it''s dangerous. Use fgets instead. And secondly, the format string you want for fscanf is "%[^\n]". as in:
fscanf (fstorage, "%[^\n]", name[count]);fscanf (fstorage, "%[^\n]", telnum[count]);

Check your documentation on the format specifiers for the *scanf/*printf line of functions for more information.
Advertisement
Here is an idea.
You can use fputc and fgetc with a terminating flag such as 0



Thanks guyz

LOL at anonymous poster
Im using fordperfects way (becuase its easiest)

This is the loop i use to read the file:

        for(count=1;count<100;count++){fscanf (fstorage, "%[^\n]", name[count]);fscanf (fstorage, "%[^\n]", telnum[count]);}        


The problem is now that when it hits the first newline char it stops reading everything so i just end up with name[1] with the correct stuff in and the rest of the array filled with nothing.

Any ideas?

Thanks in advance...




Edited by - CoiN on July 12, 2000 3:27:02 PM
Ah, this is an oversight on my part. the fscanf calls aren''t advancing the file pointer past the newline. Try something like:
for(count=1;count<100;count++)    //I assume you know what you''re doing and really do mean 1 to 100 and not 0 to 99{    fgets(fstorage, buffsize, name[count]);    name[count][strlen(name[count]) - 1] = ''\0'';    //strip hard return    fgets(fstorage, buffsize, telnum[count]);    name[count][strlen(telnum[count]) - 1] = ''\0'';    //strip hard return}        

This topic is closed to new replies.

Advertisement