File > Array

This is a discussion on File > Array within the C Programming forums, part of the General Programming Boards category; Hey guys, I tried searching, but could not fix the problem I'm having. I basically want to read in from ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    File > Array

    Hey guys,
    I tried searching, but could not fix the problem I'm having. I basically want to read in from a file that has data delimited by a ; . But its not printing any of the array values into the 2nd file.

    I checked the return code on the fscanf (read in of values) and its only returning 1, which is not right. What am I doing wrong ?


    Text File:
    Code:
    1000;Cash;Common Shares Issued;c;4000.000000;
    3000;Common Shares Outstanding;Common Shares;c;0.000000;
    Program:
    Code:
    int ViewJournal(){
        FILE *fp;
        FILE *fp2;
        int Counter2=0, Counter=0, b=0;
        int Account[MAX_ITEMS]={0};
        char Description[MAX_ITEMS][31]={0};
        char TransType[MAX_ITEMS]={0};
        double Amount[MAX_ITEMS]={0};
        char AcctDescription[MAX_ITEMS][31]={0};
        fp = fopen("tr.dat", "r");
        if (fp != NULL){
        while (fscanf(fp,"%d[^;];%30[^;];%30[^;];%2[^;];%lf[^;\n]%*c", &Account[Counter], AcctDescription[Counter], Description[Counter], TransType[Counter], &Amount[Counter] == 5);
              counter++;
        }
        fclose(fp);
        }
        else {
             printf("\n ERROR: FILE NOT FOUND!");
        }
        fp2 = fopen("gj.txt", "w");
        fprintf(fp2,"\n\t\t\tGeneral Journal");
        fprintf(fp2,"\n\t\tAccount\tDescription  \tDebit\tCredit");
        fprintf(fp2,"\n\t\t-------\t-------------\t-----\t------");
        for (Counter2=0;Counter2<Counter;Counter2++){
        if (TransType[Counter2] == 'c' || TransType[Counter2] == 'C'){
    	     fprintf(fp2,"\n\t\t%d\t%s\t\t\t%.2lf", Account[Counter2], Description[Counter2], Amount[Counter2]);
    	}
    	else if (TransType[Counter2] == 'd' || TransType[Counter2] == 'D'){
    	     fprintf(fp2,"\n\t\t%d\t%s\t\t%.2lf", Account[Counter2], Description[Counter2], Amount[Counter2]);
    	}
        }
        fclose(fp2);
    
    return 0;

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Start here:
    Code:
          while ( fscanf(fp,"%d;%30[^;];%30[^;];%c;%lf;%*c",
                         &Account[Counter], AcctDescription[Counter], 
                         Description[Counter], &TransType[Counter],
                         &Amount[Counter]) == 5 )
          {
             Counter++;
          }
    Last edited by Dave_Sinkula; 04-26-2005 at 11:22 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    DUDE!
    thanks soo much man

    works perfectly

    This forum rocks!

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, 01:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21