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

  1. #31
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Oh, and also, I'm not allowed to use strings when I read the names....Im supposed to read in one character at a time...

  2. #32
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by kal123456 View Post
    @nonpuz -- I used the exact code you just gave me, and now my program doesnt output anything ((((
    Well, of course it doesn't. There's no call to printf, putchar, etc. There is nothing in that sample that actually prints anything. nonpuz gave you some sample code that will help you figure out what you need to do. It is not a copy-paste solution. We don't do that around here. We are here to help you learn, and that wont happen if we just hand out complete solutions.

    Quote Originally Posted by kal123456 View Post
    Oh, and also, I'm not allowed to use strings when I read the names....Im supposed to read in one character at a time...
    Okay, then fgets is out. You will have to use fgetc. The documentation can be a bit intimidating at times. One tip for fgetc, make sure the variable you read into is declared as an int. fgetc returns an int, so it can return all possible char values, plus special values like EOF. Thus, a sample loop for reading a file with fgetc is:
    Code:
    int ch;
    while ((ch = fgetc(fp)) != EOF)
        // do something with ch
    Quote Originally Posted by kal123456 View Post
    @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!!
    I'm with Matticus here, you're coding all willy-nilly. You aren't taking the time to sit down and think through your problem before you start trying stuff. It's like trying to build an entire house without a single drawing or blueprint. You're bound to screw up, pour concrete in the wrong place, cut your lumber incorrectly, etc. The general process I follow for programming is something like:

    1. Read the problem, and make sure you understand what it is asking
    2. Figure out how to solve this by hand, on paper. Ignore things like details of opening a file, whether to use while or for loops, etc. Come up with a solution in plain English, or whatever your native language is. Note, if you don't know how to solve this yourself, there is no way to program a computer to do it.
    3. Try out your solution. Run through some examples with paper and pencil. You can make a fake "file" with data, on a separate sheet of paper, and "read" through that one char at a time. Keep track of every little step you take, write it down. That will be the basis for your algorithm.
    4. Take those steps written down in English, and translate them to pseudo code. Psuedo code should have roughly the same basic structure as your actual code, but you get to ignore the details and some of the error checking for now.
    5. Once you have your pseudo code, begin turning it into real C code. Work in small chunks, write no more than 5-10 lines at a time, then test that. By test, I mean compile your code with maximum warnings, and fix all errors and warnings, and run your code to make sure it behaves the way you expect.
    6. If you encounter problems, fix them immediately, before you move on. That way, if an error pops up, you know it was in the last 5-10 lines of code, and it's easy to fix.
    7. Continue the previous two step until you have fully implemented your solution to the problem.


    I suggest that, if you post anything back, it should be a description of how to solve the problem in English and/or pseudo code. We'll deal with the actual coding, and little issues like details of how fgetc works, once you have a working design.

  3. #33
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    I'm really sorry that I'm speeding through this, but it's due by today midnight, and I'm just really nervous -___- Thanks for clearing the fgetc thing out! I'm using that in my code now, so the getNames function works well. However, now I have a problem with my getSales function. this getSales function basically does the same thing as the getNames, only now it reads from a file of integers instead of characters, so I had to use fscanf in the getSales function...when I run it, the output looks like this:
    Code:
    
    
    
    
    
                                            2520252520222322252625223028252625304520
    30252021272524262023242028262425301035322829303515161514121512192024201810151216
    32303329000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000
    Process returned 0 (0x0)   execution time : 0.094 s
    Press any key to continue.
    instead of this:
    Code:
    4
    25 20 25 25
    20 22 23 22
    25 26 25 22
    30 28 25 26
    25 30 45 20
    30 25 20 21
    27 25 24 26
    20 23 24 20
    28 26 24 25
    30 10 35 32
    28 29 30 35
    15 16 15 14
    12 15 12 19
    20 24 20 18
    10 15 12 16
    32 30 33 29
    I don't see any problems with my code...how would i make it look like that??

    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
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    int getSales(int salesArray [][MAX_COLS_SALES]);
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       int i = 0;
       int j = 0;
       int row = 25;
       int col = 20;
    
      //Statements
       getNames (namesArray);
       getSales (salesArray);
    
       return 0;
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
        {
        //Statements
        FILE *fp;
        int i, j, c;
        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);
            }
    while ((c=fgetc(fp)) != EOF)
        {
            putchar(c);
    
    
        }
    fclose(fp);
    
    
    
    for(i=0;i<row;i++)
        {
         for(j = 0; j < col; j++)
         {
        printf("%c", namesArray[i][j]);
         }
        }
    
    
    
    
    return 0;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES])
        {
        //Statements
        FILE *fl;
        int i, j;
        int col=6;
        int row=25;
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
     if(fl == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
    
        else
            while((fscanf(fl,"%d",&salesArray[i][j]))!=EOF) //scanf and check EOF
            {
                for(i=0;i<row;i++)
            {
                for(j = 0; j < col; j++)
            {
                fscanf(fl,"%d\n", &salesArray[i][j]);
                printf("%d", salesArray[i][j]);
            }
    
            }
            }
    
    fclose(fl);
    
    return 0;
    
    }
    THANKS FOR ALL THE HELP AND THANK YOU FOR BEING PATIENT WITH ME!!
    Last edited by kal123456; 01-31-2013 at 02:41 PM.

  4. #34
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    First, indent your code properly. Messy code is hard to debug.

    Then for example look at your row and col variables and tell us how many lines and how many columns are in your file.

    There may be still other problems with your code.

    Bye, Andreas

  5. #35
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Ok, i'll indent it right now!
    and in the getSales function, i wrote:
    Code:
     
    int col=6;
    int row=25;
    so i made the max number of columns be 6, and the mas number of rows 25. I also defined MAX_COLS_SALES and MAX_ROWS_SALES at the very top of my program.
    what other problems might there be with my code?

  6. #36
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, since it's due at midnight, you have 10.5 hours left, so that's plenty of time. And skipping the "figure out how to actually solve the problem" phase is never going to work. You'll never get there by mashing away at the keyboard, hoping some random attempt will magically solve your problem. I'll stress it again: programming is about problem solving. Writing code is secondary. Solve the problem, and much of the code "writes itself".

    First, since you have your getNames function working, and getSales is very similar, consider using the same basic logic for both. Your getNames function basically does:
    Code:
    while not at end of file
        read once character
        store it in namesArray
    
    for each row in namesArray
        for each column in namesArray
            print that char
    So why not follow the same logic for getSales? Just read the whole file into salesArray, then, when you're all done with that, print out the whole array.

    Second, if you define constants at the top of your program, you should actually use them, everywhere. You only use them in some places. Instead of having local variables like row and col, just use the constants in your for loops:
    Code:
    for (i = 0; i < MAX_ROWS_SALES; i++)
    Do likewise for MAX_COLS_SALES, and for your MAX_NAMES constants

    Lastly, take a look at the output you're getting:
    Code:
    2520252520222322252625223028252625304520
    30252021272524262023242028262425301035322829303515161514121512192024201810151216
    32303329000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000
    Then, figure out how your output should look. Write it out on paper. You didn't give an example, so I'm guessing it should look something like this:
    Code:
    25 20 25 25
    20 22 23 22
    25 26 25 22
    30 28 25 26
    25 30 45 20
    30 25 20 21
    ...
    Describe, in English, the difference between what you actually see, and what you want to see. Something like: "The output I actually get is all the right numbers, but they're mashed together on one line. The output I want to see is...". That should be a huge clue to what you need to do to fix your program. Once again, you can't fix the problem if you don't understand it. Your first step must be to understand the problem.

  7. #37
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    Ohh ok, that makes sense that i would rather use what I already defined then creating new integers, thanks! and I fixed up my code, I made the getSales function look exactly like the getNames function, with a few minor changes, and now it outputs the 4 columns correctly!! I need the first row and column (in this case, the number 16 for names.txt and number 4 for sales.txt) to be ignored and not printed out....how would I do that? would I do that in the functions, or in the main?
    And yes, in the future I will try to start my assignments earlier so I can actually have some time to write my plan of action haha, it's just that it's due tonight and I only have 3 free hours cause the rest are class, so I'm REALLYYY nervous right now!!

    here's my code right now:
    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
    int getNames(char namesArray[][MAX_COLS_NAMES]);
    int getSales(int salesArray [][MAX_COLS_SALES]);
    int getTotalPersons(char namesArray[][MAX_COLS_NAMES]);
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int salesArray[MAX_ROWS_SALES][MAX_COLS_SALES] = {0};
       int i = 0;
       int j = 0;
       int row;
       int col;
    
      //Statements
    
    
       getNames (namesArray);
       getSales (salesArray);
       getTotalPersons (namesArray);
    
    
       return 0;
    }
    
    int getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp;
    int i, j, c;
    
    
    //open the sequential access file
     fp = fopen(INPUT_FILE_GET_NAMES,"r");
     if(fp == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
    while ((c=fgetc(fp)) != EOF)
    {
            putchar(c);
    
    
    }
    fclose(fp);
    
    
    
    for(i=0;i<MAX_ROWS_NAMES;i++)
        {
         for(j = 0; j < MAX_COLS_NAMES; j++)
         {
    
            printf("%c", namesArray[i][j]);
    
         }
     }
    
    
    
    return 0;
    
    }
    
    int getSales(int salesArray[][MAX_COLS_SALES])
    {
        //Statements
    FILE *fl;
    int i, j, c;
    
    
    //open the sequential access file
     fl = fopen(INPUT_FILE_GET_SALES,"r");
     if(fl == NULL)
            {
                printf("Error, can't open file!!!\n");
                exit(101);
            }
    
    while ((c=fgetc(fl)) != EOF)
    {
            putchar(c);
    
    
    }
    fclose(fl);
    
    for(i=0;i<MAX_ROWS_SALES;i++)
        {
         for(j = 0; j < MAX_COLS_SALES; j++)
         {
    
            printf("%c", salesArray[i][j]);
    
         }
     }
    
    
    
    return 0;
    
    }
    
    int getTotalPersons(char namesArray[][MAX_COLS_NAMES])
    {
    
        printf("%c", namesArray[0][0]);
    
    
    
        return 0;
    
    }

  8. #38
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by kal123456 View Post
    so i made the max number of columns be 6, and the mas number of rows 25.
    But don't you see that the data file you have shown us, only has 4 columns and 16 lines (omitting the first line)? Thus when you read in the numbers one after another you are out of sync (i.e. the position in the file doesn't correspond to the position in your array).
    The first line tells you the number of columns in the file (4) and I assume your program should use this information.

    Quote Originally Posted by kal123456 View Post
    what other problems might there be with my code?
    In getNames() you are not storing the names in the array. You will need them if you want to create the final report.

    Bye, Andreas

  9. #39
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Just copying the broken function getNames() won't help.

    Bye, Andreas

  10. #40
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    @AndiPersti Thanks, I actually got my program to print the table correctly! the only problem is that now I want the first element of the array not to be read...for example, the names.txt file looks 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
    I don't want the 16 at the top to be printed out...how do I ignore that in my program?

  11. #41
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by kal123456 View Post
    I don't want the 16 at the top to be printed out...how do I ignore that in my program?
    Read it into a variable, like num_rows. That is how many names there will be, and how many rows of data you will print out.

    Now that I think about it, you need to read the number at the top of sales.txt into a variable too. That tells you how many columns of sales data you have for each person/row.

  12. #42
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    so would i do something like this?
    Code:
    int num_rows = namesArray[0][0];
    since the first row and column of the array is 16 in this case? the teachers doesnt really want us to output the number of column of sales data I have. The number of columns was just given for our own reference.

  13. #43
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, simply use fscanf with the "%d" format specifier, to read into num_rows.

  14. #44
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53
    I JUST DID THAT AND IT WORKED!!! thanks so much!!

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, 02: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, 04:50 AM

Tags for this Thread