Thread: Converting a file to an array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Converting a file to an array

    --------------------------------------------------------------------------------

    Hi, how can i write a C program in Red Hat 9. I can't find a compiler.
    Also would you mind looking at this code and telling me if you think its ok. I need to put a file which has many columns of data (of which i only need the first two) into an array, so that i can add it to another such file. This is prob very basic, but C is a bit of a mystery to me and i'm only beginning to find my way.

    Code:
    #include<stdio.h>
    void main()
    {
    int i,j,array1[][2];
    FILE *inp;
    inp=fopen("file1.txt","r");
    for(i=0;i!=EOF;i++)
    for(j=0;j<2;j++)
    {
    fscanf(inp,"%d",&array1[i][j]);
    }
    fclose(inp);
    }
    What do you reckon? How can i show if this worked, would some simple printf command work? Oh, and to put the final array back into a file? Is that just fprintf? Thanks very much for any help you can provide.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Is there any particular reason you are using Redhat 9? Its pretty old now so if possible you should install Fedora Core. Anyhow, the the main compiler used on Linux is gcc. Most Linux distributions either include it as default, or else the installation allows you to select whether or not to install it. Redhat 9 I think does not install gcc by default but there are various ways to install it post install if you need to

  3. #3
    Sys.os_type="Unix";;
    Join Date
    Aug 2005
    Posts
    52
    Quote Originally Posted by Boucho
    --------------------------------------------------------------------------------

    Hi, how can i write a C program in Red Hat 9. I can't find a compiler.
    Also would you mind looking at this code and telling me if you think its ok. I need to put a file which has many columns of data (of which i only need the first two) into an array, so that i can add it to another such file. This is prob very basic, but C is a bit of a mystery to me and i'm only beginning to find my way.

    Code:
    #include<stdio.h>
    void main()
    {
    int i,j,array1[][2];
    FILE *inp;
    inp=fopen("file1.txt","r");
    for(i=0;i!=EOF;i++)
    for(j=0;j<2;j++)
    {
    fscanf(inp,"%d",&array1[i][j]);
    }
    fclose(inp);
    }
    What do you reckon? How can i show if this worked, would some simple printf command work? Oh, and to put the final array back into a file? Is that just fprintf? Thanks very much for any help you can provide.
    So you basically need to read in a file and append it to another file? Why don't you just put fgets in a while loop until EOF and put each line you get into the other file.
    Or in linux it would be easier just to do...

    Code:
    cat fromfile.txt >>tofile.txt
    Though I assume this is a learning exercise of sorts.
    Your current code has issues.. like main returns an int.
    And I have no clue what you're trying to achieve with this line..
    Code:
    for(i=0;i!=EOF;i++)
    Are you trying to go until it hits end of file? Because that isn't going to do it. Actually that loop would be endless on my system since EOF is defined as -1.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int array1[][2];
    That won't work.

    Actually that loop would be endless on my system since EOF is defined as -1.
    No quite endless -- eventually i would wrap around to -1. After UINT_MAX-1 loops or so.

    Here's how you would compile that program with gcc:
    Code:
    $ gcc -o program program.c
    You can add extra options to give you better warnings:
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 -o program program.c
    You shouldn't use void main(); use int main() instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE *fp;
        int array[1][5];
        int i,ch,j;
        
        if((fp = fopen("file.txt","r"))==NULL)
        {
            printf("Error: File cannot be opened");
            getchar();
            return 1;
        }
        else
        {
            for(i=0 ;i<2;i++)
            {
                for(j=0;j<5;j++)
                    fscanf(fp,"%d",&array[i][j]);
            }
        }
        
        for(i=0;i<2;i++)
        {
            for(j=0;j<5;j++)
                printf("%d\t",array[i][j]);
            printf("\n");
        }
        
        fclose(fp);
        getchar();
        return 0;
    }
    /* my output
    1       2       3       4       5
    6       7       8       9       10
    
    
    my File.txt
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    */
    ssharish2005

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > int array[1][5];
    Surely you mean
    int array[2][5];

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    thax Salem u are right thats my bad

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM