Thread: .txt to an array

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    3

    .txt to an array

    Hello there.
    I am a beginner (very beginner) into C programming.
    I am trying to take numbers from a .txt and put them into an array to organize them with Shellsort. I also have the problem that there are more than one vector in my .txt, and the first number represents its size, for example:
    6 44 56 25 51 11 13 (6 is not a part of it, it only means it has 6 numbers)
    4 23 51 6 31 (4 is not a part of it)
    But I am having problems trying to fullfill my arrays. Here is what I have (without the Shellsort function, its working nice):

    Code:
    int main() {
        FILE    *textfile;
        char    ch;
        int j, i;
        
        textfile = fopen("readme.txt", "r");
        if(textfile == NULL)
            return 1;
        
        fscanf(textfile, "%d", &j);
        int arr[j]; 
    
        while((ch = fgetc(textfile))!=EOF) {
            for (i = 1; i < j; i++){
                arr[i] = ch;
    }
    }
        fclose(textfile);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Could post the text file too? If the file is too big then post a good representation of the file.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,112
    Quote Originally Posted by G4143 View Post
    Could post the text file too? If the file is too big then post a good representation of the file.
    The O/P did:
    6 44 56 25 51 11 13 (6 is not a part of it, it only means it has 6 numbers)
    4 23 51 6 31 (4 is not a part of it)
    That should sufficient to demonstrate the contents of the data file.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by yunghelor View Post
    Code:
        FILE    *textfile;
        char    ch;
    
    // ...
    
        while((ch = fgetc(textfile))!=EOF) {
    fgetc() returns an int, so ch should be an int so it can hold either a valid character or EOF.

  5. #5
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Is there newlines or is it one continuous line of numbers?

  6. #6
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Quote Originally Posted by rstanley View Post
    The O/P did:


    That should sufficient to demonstrate the contents of the data file.
    I want to know if each entry is separated with newlines or not.

  7. #7
    Registered User
    Join Date
    Dec 2022
    Posts
    3
    Quote Originally Posted by G4143 View Post
    Could post the text file too? If the file is too big then post a good representation of the file.
    I'm sorry, I don't know how to upload in replies.
    The numbers are separateds by normal spaces, but when the "vector" ends, there is a new line to start the next vector.
    Their sizes are, in order, 100, 1000, 10000, 100000

  8. #8
    Registered User
    Join Date
    Dec 2022
    Posts
    3
    Quote Originally Posted by G4143 View Post
    Could post the text file too? If the file is too big then post a good representation of the file.
    Here is a link to it on my google drive:
    Google Drive: Sign-in

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Don't use fgetc. Just use fscanf to read the integers. You'll also have to use it to detect EOF. Something like:
    Code:
    int n;
    while (fscanf(file, "%d", &n) == 1) {
        int a[n];
        for (int i = 0; i < n; ++i)
            fscanf(file, "%d", &a[i]);
        ...
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread