Like Tree2Likes

2Dimensional arrays..read from txt file PROBLEM! :O

This is a discussion on 2Dimensional arrays..read from txt file PROBLEM! :O within the C Programming forums, part of the General Programming Boards category; Have you tried to compile this because even without the -Wall flag gcc has a number of complaints Code: int ...

  1. #16
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Have you tried to compile this because even without the -Wall flag gcc has a number of complaints
    Code:
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    but then you pass it an int matrix
    Code:
    int namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
    Last edited by hex_dump; 01-30-2013 at 08:52 PM.

  2. #17
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by kal123456 View Post
    Ohhh, so then how do i change it to read characters?
    look up the scanf and fscanf function. What is the format specifier for reading characters?

    once you have that you can put your print statement to print out CHARACTERS, right after you read it in to see if its reading properly. Changed your code and it working on my system.
    Last edited by hex_dump; 01-30-2013 at 08:53 PM.

  3. #18
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Ohh ok, so I changed the %d to %c which means that now it should be printing characters instead of integers. However, now it doesn't output anything, just a blank space haha.....what do you mean by making my print statement to print out characters?....

  4. #19
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by kal123456 View Post
    Ohh ok, so I changed the %d to %c which means that now it should be printing characters instead of integers. However, now it doesn't output anything, just a blank space haha.....what do you mean by making my print statement to print out characters?....
    give your printf statment the same specifier as you did for scanf
    Code:
    for(i=0;i<row;i++)  {
         for(j = 0; j < col; j++){
             fscanf(fp,"%c", &namesArray[i][j]);
             printf("%c", namesArray[i][j]);
        }
    }

  5. #20
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    OMG.........it actually works!!!! only now, the last problem, how do I get it to look exactly like the txt file? like this:

    Code:
    16
    Kelly, Victor       
    Lam, Gary           
    Nagasake, David     
    Nguyen, Jeff        
    Nguyen, Michael     
    Sinn, Scott         
    Smith, Jacob        
    Son, Thai           
    Tavares, Maribel    
    Tran, Diane         
    Tsukamoto, Andrew   
    Wang, Mary          
    Young, Daniel       
    Wells, Steve        
    Wong, Justin        
    Johnson, Mary
    cause right now, it looks like this:

    Code:
    16Kelly,VictorLam,GaryNagasake,DavidNguyen,JeffNguyen,MichaelSinn,ScottSmith,Jac
    obSon,ThaiTavares,MaribelTran,DianeTsukamoto,AndrewWang,MaryYoung,DanielWells,St
    eveWong,JustinJohnson,Mary
    
    
    
    
    Process returned 0 (0x0)   execution time : 0.297 s
    Press any key to continue.

  6. #21
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    Code:
    if(namesArray[i][j]==',') printf("\n");

  7. #22
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by camel-man View Post
    Code:
    if(namesArray[i][j]==',') printf("\n");
    I think the comma is just a last name, first name delimeter. kinda like
    Code:
    man,camel
    kal123456, are you on a windows machine? Please post your full code you have.

  8. #23
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    612
    Woops, I dont know why I thought the file was like this
    Code:
    Kelly Victor,     
    ...
    ..
    ...      
    Tsukamoto Andrew,  
    Wang Mary,         
    Young Daniel,     
    Wells Steve,       
    Wong Justin,       
    Johnson Mary,

  9. #24
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by camel-man View Post
    Woops, I dont know why I thought the file was like this
    Code:
    Kelly Victor,     
    ...
    ..
    ...      
    Tsukamoto Andrew,  
    Wang Mary,         
    Young Daniel,     
    Wells Steve,       
    Wong Justin,       
    Johnson Mary,
    LOL, it's late where you are? let's go with that

  10. #25
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    ya im on a windows machine and here's my code:

    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    
    //Function Declarations
    char getNames(char namesArray[][MAX_COLS_NAMES]);
    
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int i = 0;
       int j = 0;
       int row = 25;
       int col = 20;
    
      //Statements
       getNames (namesArray);
    
       return 0;
    }
    
    char getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j;
    int col=20;
    int row=25;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
    else{
    
    
    
    for(i=0;i<row;i++)
        {
         for(j = 0; j < col; j++)
         {
             fscanf(fp,"%c\n", &namesArray[i][j]);
    printf("%c", namesArray[i][j]);
         }
     }
    }
    
    fclose(fp);
    
    return 0;
    
    
    
    }

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    2,381
    So your program is not printing newlines to the screen?

    Code:
        fscanf(fp,"%c\n", &namesArray[i][j]);
        printf("%c", namesArray[i][j]);
    Quote Originally Posted by fscanf
    the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
    "fscanf()"
    For plucking single characters out of a text file, try "fgetc()".

  12. #27
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    no it doesnt!! i had to add what @camel-man suggested,
    Code:
     if(namesArray[i][j]==',') printf("\n");
    However, now it outputs a slightly different output, the commas are misarranged!! it looks like this now!
    Code:
    16Kelly,
    VictorLam,
    GaryNagasake,
    DavidNguyen,
    JeffNguyen,
    MichaelSinn,
    ScottSmith,
    JacobSon,
    ThaiTavares,
    MaribelTran,
    DianeTsukamoto,
    AndrewWang,
    MaryYoung,
    DanielWells,
    SteveWong,
    JustinJohnson,
    Mary
    
    
    
    Process returned 0 (0x0)   execution time : 0.078 s
    Press any key to continue.
    this is my whole program code:
    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    
    //Function Declarations
    char getNames(char namesArray[][MAX_COLS_NAMES]);
    
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int i = 0;
       int j = 0;
       int row = 25;
       int col = 20;
    
      //Statements
       getNames (namesArray);
    
       return 0;
    }
    
    char getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j;
    int col=20;
    int row=25;
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
    else{
    
    
    
    for(i=0;i<row;i++)
        {
         for(j = 0; j < col; j++)
         {
             fscanf(fp,"%c\n", &namesArray[i][j]);
            printf("%c", namesArray[i][j]);
            if(namesArray[i][j]==',') printf("\n");
         }
     }
    }
    
    fclose(fp);
    
    return 0;
    
    
    
    }
    How do I use the fgetc()? do i just put that instead of printf()?...
    Btw, THANKS GUYS SO MUCH FOR YOUR HELP!!! WOULDNT BE ABLE TO DO THIS WITHOUT YOU! :P

  13. #28
    Registered User
    Join Date
    Jun 2011
    Posts
    2,381
    You just need to slow down. Stop adding code all willy-nilly!

    You have the input (post #20).

    You added the code to print a newline after each comma (post #21), despite the fact that:

    1. This post was subsequently corrected as an error (posts #22 and #23)
    2. Based on the input (post #20), you shouldn't want a newline after each comma

    How do I use the fgetc()? do i just put that instead of printf()?...
    "fgetc()" (like "fscanf()") reads from an input stream. "printf()" prints to the output. So no, you wouldn't put a read in place of a write.

    Did you read the link provided for "fgetc()"? Pay particular attention to the "return value" paragraph.

  14. #29
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Using fgets or fgetc will fix all your problems (I would recommend fgets here as you are reading lines, not single characters logically.).

    Here is how you would use it in your case:
    Code:
    char buf[SOME_SIZE];
    FILE * fp;
    char multi_array[5][SOME_SIZE];
    
    if (fp = fopen("somefile.txt", "r")) {
        int i = sizeof multi_array / sizeof multi_array[0] - 1;
        while (fgets(buf, sizeof buf, fp) && i >= 0)
            strcpy(multi_array[i--], buf);
        fclose(fp);
    }
    That should give you an idea of how to get going.

  15. #30
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @Matticus -- Yes, i just read the link!! I tried doing what it said in the link, but my code got all messed up, so I'm trying to see if nonpuz's way will work....I'm so confused!!

    @nonpuz -- I used the exact code you just gave me, and now my program doesnt output anything ((((

    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    #define SOME_SIZE 500
    
    //Function Declarations
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int i = 0;
       int j = 0;
       int row = 25;
       int col = 20;
    
      //Statements
       getNames (namesArray);
    
       return 0;
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j;
    int col=20;
    int row=25;
    char buf[SOME_SIZE];
    
    //open the sequential access file
     if (fp = fopen(INPUT_FILE_GET_NAMES,"r")){
     int i = sizeof namesArray / sizeof namesArray[0] - 1;
     while (fgets(buf, sizeof buf, fp) && i>=0)
        strcpy(namesArray[i--], buf);
     fclose(fp);
    
     return 0;
     }
    }

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read data from file into 2 dimensional arrays help
    By khoavo123 in forum C Programming
    Replies: 4
    Last Post: 02-02-2012, 01:30 AM
  2. Reading Picture file into 2dimensional data array?
    By DiscoStu9 in forum C Programming
    Replies: 10
    Last Post: 08-25-2009, 06:03 PM
  3. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  4. Replies: 4
    Last Post: 01-30-2005, 03:50 AM

Tags for this Thread


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