0

I have a file called pi.txt which contains, as you probably imagine, the numbers of pi.

The format goes like this

Line 1: 3. Line 2: 14159265358979323846264338327950288419716939937510 Line 3: 58209749445923078164062862089986280348253421170679

and it continues to

Line 20001: 56787961303311646283996346460422090106105779458151

There are 1020001 digits minus the 3 and the .(of 3.14xxxxxx...)

I have to read this file to an array (not a 2D one. What I want to do later will become much harder if I use a 2D array as I imagine it). This is my code:

void fpi();
char **arraypi;

int main(int argc, char *argv[]) {
    fpi();
    int i;
    while (i<=10){
    printf("%d", arraypi[i++]);}
    return 0;
}


void fpi(){
    char pi[1020001];
    arraypi = malloc(1020001 * sizeof(int));
    FILE *file;
    int i=0;
    file = fopen("pi.txt", "r");
    if (file == NULL){
        printf("The file cannot be opened.\n");
        exit(1);
    }
    while (fscanf(file, "%c", &pi)==1){
            strcpy(arraypi[i++], pi);
    }
    fclose(file);
}

I get a segmentation fault and I can't figure out why. I'm sure it has to do with the pointers I'm using and fscanf.

user3601507
  • 173
  • 1
  • 2
  • 8
  • 1
    Your `pi` array is *huge* and *stack allocated*, and you are only reading one character into it! This may not crash if you're lucky enough to have 8MB of stack, but it's still a terrible idea. – nneonneo Jun 10 '14 at 06:05
  • possible duplicate of [Reading the whole text file into a char array in C](http://stackoverflow.com/questions/3747086/reading-the-whole-text-file-into-a-char-array-in-c), as per discussion with the OP in my answer section. Another dupe [here](http://stackoverflow.com/questions/410943/reading-a-text-file-into-an-array-in-c). – legends2k Jun 10 '14 at 06:23
  • It's not a 'big' number, but 'long' number. – zoska Jun 10 '14 at 08:13
  • English is not my first language. – user3601507 Jun 10 '14 at 10:11

1 Answers1

0

strcpy expects you to give a null terminated character array (string) and you're giving an array of 1020001 characters where except the first character, which is read from the file, everything else is undetermined.

char pi[1020001];
while (fscanf(file, "%c", &pi)==1){   // only one character gets copied to pi
        strcpy(arraypi[i++], pi);     // strcpy will expect pi to be a string and not just a char
}

Hence it may go on an infinite loop and write beyond the allowed memory allocated for it, thereby corrupting the stack structure and thus leading to the segmentation fault. You're also treading on undefined behaviour when you're doing this.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Can I use `fgets` to store a single number into a single cell? – user3601507 Jun 10 '14 at 06:14
  • What do you basically want to do? Read the contents of the file onto a character array or integer array? – legends2k Jun 10 '14 at 06:18
  • Ah yes I forgot to mention that. I want to store them into a character array and print the first and last 20 digits and then compare them with the file's contents and make sure they match. – user3601507 Jun 10 '14 at 06:21
  • @user3601507: See my comment in the question section, and before posting a question, please try to [search enough](https://www.google.com/search?q=site%3Astackoverflow.com+[c]+read+a+file+to+character+array) and see if it's already been asked. **Hint:** 99% of the time, it's already asked and solved. – legends2k Jun 10 '14 at 06:23
  • My keywords must have been pretty bad because I didn't find the same question. I will read it now and see if I can get it to work. – user3601507 Jun 10 '14 at 06:29
  • The question you posted solved my problem but the thing is it also prints `3.`. I don't want that in the array. I don't know enough on how to use `fseek`, `ftell` to understand. – user3601507 Jun 10 '14 at 06:45
  • The explanation for your questions would go beyond a comment box. Please read a [proper C tutorial](http://www.google.com/search?q=c+programming+tutorial) and get an understanding of the basics. This site is for people who know that and is stuck in trying to do something specific. – legends2k Jun 10 '14 at 06:52
  • Well for now I guess I can just use `arraypi[3 + i++]` to do my work. – user3601507 Jun 10 '14 at 06:56