Home > Enterprise >  How do i copy n characters for m lines in a file (in c)?
How do i copy n characters for m lines in a file (in c)?

Time:05-07

I have a file of 1000 lines with up to 70 characters in them. I want to copy the 13 first in a table, the 15th-30th in another table... i tried many different things without success, here's one of them :

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

#define size_num 13  // 13 character with \0 included
#define line 1000
#define carac 300 // like 300 characters per line

int verifFile(FILE*pf)    //check file
{
    if (pf == NULL)
    {
        printf("can't open file");
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    float td,tf;
    td=clock(); //count execution time of program
    FILE* pf=NULL;
    pf = fopen("call.txt", "r");
    verifFile(pf);
    char str[line];
    char number[line][size_num];
    while(fgets(str,line,pf)!= NULL)
    {
        printf("%s", str);
        number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
                                            //of each 1000 lines
    }
    printf("\nsecond : \n"); // separates the printings
    for(int i;i<1001;i  )
    {
        for(int j;j<14;j  )
        {
            printf("%c",number[i][j]); //supposed to print each charac stored (doesnt work)
        }
    }
    tf=clock();
    printf("\n execution time : %f secondes",(tf-td)/CLOCKS_PER_SEC);   //display execution time
    return 0;
}

there are no warnings so i dont know what to do :/

CodePudding user response:

The way you copy the substrings is broken:

    while(fgets(str,line,pf)!= NULL)
    {
        printf("%s", str);
        number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
                                            //of each 1000 lines
    }

First of all, you always assign to number[1000][13] which is illegal as number only has 1000 rows, i.e. maximum index is 999. Also each row only has 13 elements making 12 the maximum index.

Same out of bounds access happens for str.

Then you do not have any counter indicating in which element of your array you want to copy the string.

Finally, strings cannot be copied by a simple assignment. You need to use strcpy or strncpy.

This loop should look like that:

    int row = 0;
    while(fgets(str,line,pf)!= NULL)
    {
        printf("%s", str);
        strncpy(number[row], &str[0], size_num);
        number[row][size_num-1] = 0; // Don't forget to terminate the string!

        // Add copying the other parts here as well...

        row  ;
    }

Then you also have a problem with printing the content:

    for(int i;i<1001;i  )
    {
        for(int j;j<14;j  )
        {
            printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
        }
    }

Again, you have out of bounds accesses. And you print all possible lines even if the file only contains a few lines. Also you do not even initialize your loop counters to 0.

Do this instead:

    for(int i = 0; i<row;i  )
    {
#if 0
// use this part if you really want to print char by char...
        for(int j = 0; j<size_num; j  )
        {
            printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
        }
#else
// Use this if you want to print the string at once.
        printf("%s\n",numero[i]);
#endif
    }
  •  Tags:  
  • c
  • Related