Thread: Read In From Many Files In One Function

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Unhappy Read In From Many Files In One Function

    ok can someone please tell what is wrong with this code. i am trying to read in 3 files into 3 different char arrays. i want to call one function (3 times) and pass in the filename and name of the char array each time. here are my errors from my compile. below is the code.

    ERRORS:
    assign.c: In function `main':
    assign.c:59: warning: passing arg 2 of `readFile' from incompatible pointer type
    assign.c: At top level:
    assign.c:117: conflicting types for `readFile'
    assign.c:47: previous declaration of `readFile'
    assign.c: In function `readFile':
    assign.c:136: subscripted value is neither array nor pointer
    assign.c:141: subscripted value is neither array nor pointer
    assign.c:147: subscripted value is neither array nor pointer

    DECLARATION:
    void readFile(char *read_file, char *read_to_array);

    CALL TO FUNCTION:
    readFile("street_name.txt", comp_street_name);
    readFile("street_type.txt", comp_street_type);
    readFile("suburb_name.txt", comp_suburb_name);

    FUNCTION:
    Code:
    void readFile(char *read_file, char read_to_array)
    void readFile(char *read_file, char read_to_array)
    {
    char c;
    int i = 0, j = 0, eof_test = FALSE;
    file = fopen(read_file, "r");
    if(file == NULL)
    {
    printf("Cannot open input file, please check the filename try again.\n");
    printf("Input files should be named: street_name.txt, street_type.txt, & suburb_name.txt.\n\n");
    exit(1);
    }
    while(eof_test == FALSE)
    {
    for(i = 0; i < MAX_SIZE; i++)
    {
    for (j = 0; j < STRING_MAX_SIZE; j++)
    {
    c = fgetc(file);
    if(c == '\n')
    {
    read_to_array[i][j] = (char) NULL;
    break;
    }
    else if(c == EOF)
    {
    read_to_array[i][j] = (char) NULL;
    eof_test = TRUE;
    break;
    }
    else
    {
    read_to_array[i][j] = c;
    }
    }
    }
    }
    fclose(file);
    }

    i know the problem is where i try "read_to_array[i][j] = c;" can someone help me out with this annoying stuff.

    thanks heaps.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know the problem is that you've used code tags incorrectly. Here you go:

    Quote Originally Posted by kermi3
    Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    Make sure you have a look at the the posting guidelines:
    http://cboard.cprogramming.com/annou...ouncementid=51
    Following the rules will ensure you get a prompt answer to your question.

    Remember, too, that the board has a search facility, a link is at the top of your screen:
    http://cboard.cprogramming.com/search.php
    It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

    It appears that you are posting a homework assignment or other project.

    Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

    Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.


    On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

    Feel free to PM me with any questions.

    Good Luck,

    Kermi3

    Read and follow the above instructions, and try your post (in the same thread) again.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    more plz

    thanks for the small bit of help. this is a smallest part of one big assignment, therefore i would appreciate if u could elaborate on your answer to me using the code tags incorrectly. maybe an example would do the trick, or even a pointer using my own code or something.

    thanks in advance.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, you mean like the link I provided which says in red code tags? Basicly, you put one opening tag before all of your code, and one closing tag after all of your code.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Unhappy Still More Plz

    ok can someone please tell what is wrong with this code. i am trying to read in 3 files into 3 different char arrays. i want to call one function (3 times) and pass in the filename and name of the char array each time. here are my errors from my compile. below is the code.

    ERRORS:
    assign.c: In function `main':
    assign.c:59: warning: passing arg 2 of `readFile' from incompatible pointer type
    assign.c: At top level:
    assign.c:117: conflicting types for `readFile'
    assign.c:47: previous declaration of `readFile'
    assign.c: In function `readFile':
    assign.c:136: subscripted value is neither array nor pointer
    assign.c:141: subscripted value is neither array nor pointer
    assign.c:147: subscripted value is neither array nor pointer

    DECLARATIONS:
    Code:
    void readFile(char *read_file, char *read_to_array);
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    char comp_street_type[MAX_SIZE][STRING_MAX_SIZE];
    char comp_suburb_name[MAX_SIZE][STRING_MAX_SIZE];
    CALL TO FUNCTION:
    Code:
    readFile("street_name.txt", comp_street_name);
    readFile("street_type.txt", comp_street_type);
    readFile("suburb_name.txt", comp_suburb_name););
    FUNCTION:
    Code:
    void readFile(char *read_file, char *read_to_array)
    {
       char c;                                           /* Declare temporary read in variable */
       int i = 0, j = 0, eof_test = FALSE;               /* Declare temporary counting variables */
       file = fopen(read_file, "r");                     /* Open source file for reading using parameter */
       if(file == NULL)                                  /* Check if read file is corrupted */
       {
           printf("Cannot open input file, please check the filename try again.\n"); /* Print error message */
           printf("Input files should be named: street_name.txt, street_type.txt, & suburb_name.txt.\n\n"); /* Print error message */
           exit(1);                                      /* Exit due to read file error */
       }
       while(eof_test == FALSE)                          /* Continue read loop while end of file is not found */
       {
          for(i = 0; i < MAX_SIZE; i++)                  /* Run loop until maximum address book size */
          {
             for (j = 0; j < STRING_MAX_SIZE; j++)       /* Run loop until maximum string size */
             {            c = fgetc(file);                         /* Gather character from file to temporary variable */
                if(c == '\n')                            /* Check if read in character is the end of line */
                {
    //               read_to_array[i][j] = (char) NULL;    /* Place null in data array field if end of line */
                   break;                                /* Break from for loop for new line */
                }                                        /* End if statement */
                else if(c == EOF)                        /* Check if read character is the end of file */
                {
    //               read_to_array[i][j] = (char) NULL;    /* Place null in data array field if end of file */
                   eof_test = TRUE;                      /* Set end of file tester to true */
                   break;                                /* Break from for loop if true */
                }
                else                                     /* Else write the read character to data field */
                {                                        /* Start else statement */
    //               read_to_array[i][j] = c;              /* Move read in character into array */
               }
             }
          }
       }   fclose(file);
    }
    basically im getting those errors where the // comments are, i cant seem to get the right call to the global variables to put the info into. thanks again.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    haee man look at the function level readfile

    Code:
    void readFile(char *read_file, char *read_to_array)
    {
       char c;                                           /* Declare temporary read in variable */
       int i = 0, j = 0, eof_test = FALSE;               /* Declare temporary counting variables */
       file = fopen(read_file, "r");                     /* Open source file for reading using parameter */
       if(file == NULL)                                  /* Check if read file is corrupted */
       {
           printf("Cannot open input file, please check the filename try again.\n"); /* Print error message */
           printf("Input files should be named: street_name.txt, street_type.txt, & suburb_name.txt.\n\n"); /* Print error message */
           exit(1);                                      /* Exit due to read file error */
       }
       while(eof_test == FALSE)                          /* Continue read loop while end of file is not found */
       {
          for(i = 0; i < MAX_SIZE; i++)                  /* Run loop until maximum address book size */
          {
             for (j = 0; j < STRING_MAX_SIZE; j++)       /* Run loop until maximum string size */
             {            c = fgetc(file);                         /* Gather character from file to temporary variable */
                if(c == '\n')                            /* Check if read in character is the end of line */
                {
    //               read_to_array[i][j] = (char) NULL;    /* Place null in data array field if end of line */
                   break;                                /* Break from for loop for new line */
                }                                        /* End if statement */
                else if(c == EOF)                        /* Check if read character is the end of file */
                {
    //               read_to_array[i][j] = (char) NULL;    /* Place null in data array field if end of file */
                   eof_test = TRUE;                      /* Set end of file tester to true */
                   break;                                /* Break from for loop if true */
                }
                else                                     /* Else write the read character to data field */
                {                                        /* Start else statement */
    //               read_to_array[i][j] = c;              /* Move read in character into array */
               }
             }
          }
       }   fclose(file);
    }
    you are sending the ttwo dim array to function but the function take only one dim. i should be
    [code]
    void readFile(char *read_file, char **read_to_array)
    [code]

    have looked at the function. you have declared the file pointer
    Code:
    FILe *file;
    hope this helps

    s.s.harish

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    sorry about the code tags

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) You cannot test EOF with a char. It's covered in the FAQ.
    2) Don't typecast NULL. There's no point to doing so, it's not needed.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Red face Update

    ok we r getting closer here (i can feel it)... just a little more help from everyone plz.

    DECLARATION:
    Code:
    void readFile(char *read_file, char **read_to_array);
    FILE *file = NULL;
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    FUNCTION CALL:
    Code:
    readFile("street_name.txt", comp_street_name);
    FUNCTION CODE:
    Code:
    void readFile(char *read_file, char **read_to_array)
    {
       char z;
       int i = 0, j = 0, eof_test = FALSE;
       file = fopen(read_file, "r");
       if(file == NULL)
       {
          exit(1);
       }
       while(eof_test == FALSE)
       {
          for(i = 0; i < 20; i++)
          {
              for (j = 0; j < 256; j++)
              {
                  z = fgetc(file);
                  if(z == '\n')
                  {
                      read_to_array[i][j] = (char) NULL;
                      break;
                  }                                        /* End if statement */
                  else if(z == EOF)
                 {
                   read_to_array[i][j] = (char) NULL;
                   eof_test = TRUE;
                   break; 
                 } 
                 else 
                  {
                     read_to_array[i][j] = z;
                  }
             }
          }
       }
       fclose(file);
    }
    ERRORS:
    Code:
    readFile("street_name.txt", comp_street_name);
    Error: need explicit cast for function parameter 2 to get from: char (*)[256] to char**

    --> obviously the error is in the function call, i just cant seem to get what im supposed to pass into the function. the function code itself seems to be right now (inparticular where ive bolded it). everything else works pretty much fine, so im not looking to mess with everything - just the error line.

    thanks so much to everyone who has replied.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got your function prototype and definition wrong, as it states. You should have:
    Code:
    void readFile(char *read_file, char read_to_array[][STRING_MAX_SIZE]);
    FILE *file = NULL;
    char comp_street_name[MAX_SIZE][STRING_MAX_SIZE];
    
    ...
    
    void readFile(char *read_file, char read_to_array[][STRING_MAX_SIZE])
    {
       char z;
    ...
    Or if you like, you can fill in the left parameter in both places also.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    ok i think i almost got it now... just wanna clear up how i call the function.

    would this be right?
    Code:
    readFile("street_name.txt", comp_street_name);

    or do i need to do this, and if so what do i put in the question marks?
    Code:
    readFile("street_name.txt", comp_street_name[???][???]);
    thankyou again

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    originally posted by djwicks
    Code:
    ok i think i almost got it now... just wanna clear up how i call the function.
    
    would this be right?
    
    Code:
    readFile("street_name.txt", comp_street_name);
    
    
    or do i need to do this, and if so what do i put in the question marks?
    
    Code:
    readFile("street_name.txt", comp_street_name[???][???]);
    
    thankyou again

    this is right
    Code:
    readFile("street_name.txt", comp_street_name);
    s.s.harish

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    THANKYOU so much, everybody who has cared enough to help.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf function won't read my float value...?
    By qpsnhalfs in forum C Programming
    Replies: 8
    Last Post: 07-07-2009, 11:01 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM