Thread: Doesn't work to save my arrays to a text file!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Unhappy Doesn't work to save my arrays to a text file!

    Hello.

    Recently started to learn some basic programming.

    Problem: There is a file, which we will call ultra.dat.
    This numbers.dat have lots of numbers and combinations with arrays of [151][201]. I have so far written a code that will read the text file and print them on the screen. This have worked so far and it prints all the numbers exactly in the numbers.dat. Now, the next step for it is to re-arrange the combinations and put them in arrays of [151][201] and save the file named ultra1.dat.

    Problem here is that it doesnt put them in arrays. I have tried so many ways but it simply just don't work. How do I accomplish it?

    thanks!

    Code:
    #include <stdio.h>
    int main (void){
    FILE *ultra;
    FILE *ultraskriv;
    
    int tal, antal,i,j;
    int Matris[151][201];
    
        ultra=fopen("ultra.dat", "r");
    
            for(i=1;i<=(151);i++)
                for(j=1;j<=(201);j++){
                    fscanf(ultra, "%d", &antal);
                        printf("%5d", antal);
    
        }
    ultraskriv=fopen("ultra1.dat","w");
            for(i=1;i<=(151);i++){
            for(j=1;j<=(201);j++)
            fprintf(ultraskriv,"%d", &Matris[i][j]);
            fclose(ultraskriv);
            }
    
        fclose(ultra);
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, i guess you should also print some spaces every time you more on to the next column and newlines every time you move on to the next row.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Try:
    Code:
    for(i=0;i<151;i++)
                for(j=0;j<201;j++){

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by BillyTKid View Post
    Try:
    Code:
    for(i=0;i<151;i++)
                for(j=0;j<201;j++){
    Thanks for your effort but this doesn't solve the problem.

    Want me to upload the dat file?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    ultraskriv=fopen("ultra1.dat","w");
            for(i=1;i<=(151);i++){
            for(j=1;j<=(201);j++)
            fprintf(ultraskriv,"%d", &Matris[i][j]);
            fclose(ultraskriv);        }
    You close the file at soon as you print the first row of data!!
    Devoted my life to programming...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    And don't forget about spaces and newlines
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    1
    I am working on arrays ? I want to find 3 consecutive occurences of 0xaa in my array.
    Array starting address (0x100 to 0x400).
    all i need to do to determine how much stack has been used is to “walk” through the memory, starting at the top (0x4FF), until we find an area containing 0xAA. Now, a single 0xAA could still be in the stack, so we will require 3 consecutive 0xAA values to determine that the top of the stack has been reached. Then we need to account for the “extra” 0xAA values we just read. Simple!


    utility finds the first three occurances of the prewritten
    test pattern 0xAA.

    Any help?

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Sipher View Post
    Code:
    ultraskriv=fopen("ultra1.dat","w");
            for(i=1;i<=(151);i++){
            for(j=1;j<=(201);j++)
            fprintf(ultraskriv,"%d", &Matris[i][j]);
            fclose(ultraskriv);        }
    You close the file at soon as you print the first row of data!!
    Still doesn't work when I change it. I have tried to remove fclose(ultraskriv); and/or write it and the end of the code. Yet doens't work....

    I mean, do you see anything wrong with the code?

    I have uploaded http://www.2shared.com/file/zg_CvohL/ultra.html the ultra.dat file and if someone can, try to copy the code I have written it to your c progrmmaing and put the dat file in the same map as the coding! I appreciate all kind of help, even if for small tips.

    Between, is there any neccesity to add Matris[151][201]=0; under

    Code:
    int tal, antal,i,j;
    int Matris[151][201];
    
        ultra=fopen("ultra.dat", "r");
    
            for(i=1;i<=(151);i++)
                for(j=1;j<=(201);j++){
                    fscanf(ultra, "%d", &antal);
                        printf("%5d", antal);
                            Matris[151][201]=0;     }
    ?
    Last edited by zergling; 10-23-2010 at 11:39 AM.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You mean you don't get the output you expect or that you don't get any output?
    Devoted my life to programming...

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    Between, is there any neccesity to add Matris[151][201]=0; under
    Adding that will certainly crash your program!
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Sipher View Post
    You mean you don't get the output you expect or that you don't get any output?

    I don't get the expected output. Sorry if I didn't clarify that :-) I get like 265512, 265516, 265520 etc up to 40 pages or so... which is not the expected output.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Sipher View Post
    Code:
    Between, is there any neccesity to add Matris[151][201]=0; under
    Adding that will certainly crash your program!
    You're right :-)

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Then i repeat, add the necessary spaces and/or newlines in each fprintf call!
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Sipher View Post
    Then i repeat, add the necessary spaces and/or newlines in each fprintf call!
    I'm sorry if I'm starting to get tedious, but what do you mean my friend?

    Do you mean newline = \n?

  15. #15
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by zergling View Post
    Do you mean newline = \n?
    Yes!!!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM