简体   繁体   中英

Reading multiple lines of data from a file

I am reading data from a pre-read file and storing it in a buffer in which I have ran through a struct to organise the data to then re-save it in another file.

However I am only reading one line of code.

My Code - Opening File:

File *p_file

char fileLocation[40];
char buff[1000];

printf("\nEnter file name: \n");
scanf("%s, fileLocation);

p_file = fopen(fileLocation, "r");

if(!p_file)
{
    printf("\nError!\n");
}

Loop to read data and save file

while(fgets(buff, 1000, p_file)!=NULL
{
    printf("%s", buff);

    storedData.source = atoi(strtok(buff, ":");
    storedData.destination = atoi(strtok(0, ":");
    storedData.type = atoi(strtok(0, ":");
    storedData.port = atoi(strtok(0, ":");
    storedData.data = strtok(0, ":\n");

    printf("\nEnter a File Name to save:");
    scanf("%s", fileLocation);

 if ((p_file = fopen(fileLocation, "w")) == NULL
 {
    puts("\n Could not point to the file.");
 }else{
    printf("\nSaving");
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n",
            storedData.source,
            storedData.destination,
            storedData.type,
            storedData.port,
            storedData.data );

    fclose(p_file);
 }
fclose(p_file);

Current output of data:

0001:0002:0003:0021:CLS

Wanted output of data:

0001:0002:0003:0021:CLS
0001:0010:0003:0021:CLS
0001:0002:0002:0080:<HTML>

I believe I must declare an integer value to use to loop through the file contents as well as use malloc to get the size of the struct but I do not know how I would go about doing this. Any help will be much appreciated.

You are overusing p_file for file to read as well as file to write.

if ((p_file = fopen(fileLocation, "w")) == NULL)

With this you are loosing pointer for file that you opened for reading. And as you close it in the else part fgets() thinks there are no more lines.

Use some other variable for file to write.


If you want to work on buffered data then change your while(fgets()... to read all lines and then work on each line. fgets() will not read multiple lines.

Your loop actually do the thing only one time

storedData.data = strtok(0, ":\\n");

so you just take the first line.

FILE *in_file;
FILE *out_file;
char fileLocation[40];
char buff[1000];

printf("\nEnter file name: \n");
if( 1 != scanf(" %s, fileLocation) )
{
    perror( " scanf failed for input file: );
    exit( EXIT_FAILURE );
}

if( NULL == (in_file = fopen(fileLocation, "r") )
{
    perror( "fopen failed for input file" );
    exit( EXIT_FAILURE );
}

printf("\nEnter a File Name to save:");
if( 1 != scanf(" %s", fileLocation) )
{
    perror( "scanf failed for outut file name" );
    fclose( in_file ); // cleanup
    exit( EXIT_FAILURE );
}

if ( NULL == (out_file = fopen(fileLocation, "w")) )
{
    perror( " fopen failed for output file" );
    fclose( in_file ); // cleanup
    exit( EXIT_FAILURE )l
}

while(fgets(buff, 1000, p_file)!=NULL) )
{
    printf("%s", buff);

    storedData.source = atoi(strtok(buff, ":");
    storedData.destination = atoi(strtok(0, ":");
    storedData.type = atoi(strtok(0, ":");
    storedData.port = atoi(strtok(0, ":");
    storedData.data = strtok(0, ":\n");

    printf("\nSaving");
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n",
        storedData.source,
        storedData.destination,
        storedData.type,
        storedData.port,
        storedData.data );
} // end while

fclose( in_file );  // cleanup
fclose( out_file ); // cleanup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM