Thread: Reading from a file to string using a function

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    Reading from a file to string using a function

    Hello everyone!
    I have a school project Im having issues with, well just one part rather. Here is my code im trying to implement as a function that reads a company address from a file: (this code is functional
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef char STR30[30]; // programmer defined data type
    typedef STR30 STR30Address[3]; // another pddt ...
    using namespace std;
    
    
    int main(void)
    {
            STR30Address info;
            FILE * checkInFile;     
            int i;                  
            checkInFile = fopen("./CompanyInfo.txt","rt");
            if (checkInFile == NULL)                        
            {
              printf("  Can't open check descriptions file ...\n");
              while (getchar() != '\n');
              exit(-300); // reqs <stdlib.h>
            }
        for(i=0; i < 3; i++)
            {
            fgets(info[i],80,checkInFile); //read max 80 bytes from current rec.
             info[i][strlen(info[i])-1] = 0; // replace /n with /o terminator
            }
        fclose(checkInFile);
            for(i=0; i < 3; i++)
              printf("   %-30s\n",info[i]);
            fflush(stdin),getchar();
            return 0;
    }

    And this is my attempt at the function with the variable info[3][30] passed by reference (not working right):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef char STR30[30]; // programmer defined data type
    typedef STR30 STR30Address[3]; // another pddt ...
    using namespace std;
    void getCompanyInfo(STR30Address *info);
    int main(void)
    {
            STR30Address info;;
            getCompanyInfo(&info);
            for(int i=0; i < 3; i++)
              printf("   %-30s\n",info[i]);
            fflush(stdin),getchar();
            return 0;
    }
    void getCompanyInfo(STR30Address *info)
    {
    
    
            FILE * checkInFile;
            int i;                  //loop counter
            checkInFile = fopen("./CompanyInfo.txt","rt");
            if (checkInFile == NULL)
            {
              printf("  Can't open check descriptions file ...\n");
              while (getchar() != '\n');
              exit(-300); // reqs <stdlib.h>
            }
        for(i=0; i < 3; i++)
            {
            fgets(*info[i],35,checkInFile); //read max 35 bytes from current rec.
             *info[i][strlen(*info[i])-1] = 0; // replace /n with /o terminator
            }
        fclose(checkInFile);
    }

    Its giving me this output:
    Code:
    Sabre Corporation
                
       Uҋ                           
       ?I?Ȉ                     
    
    
    Segmentation fault (core dumped)

    It should be a 3 line address pulled from a txt file.
    My suspicion is the fgets line.

    Any Help would be greatly appreciated!
    Last edited by micker2g; 12-09-2012 at 01:18 AM.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    I think the problem is with the parenthesis, you are dereferencing the pointer wrongly. Try changing your code to this:
    Code:
    fgets((*info)[i],35,checkInFile);
    (*info)[i][strlen((*info)[i])-1] = '\0';
    Also, check why fflush(stdin) is wrong.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Thank you! This worked like a charm. And as for fflush(stdin), I did not know that. Good to know.
    Last edited by micker2g; 12-09-2012 at 08:46 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    You're welcome!
    Glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading a string file into a 2d array
    By mshel130 in forum C Programming
    Replies: 4
    Last Post: 11-16-2011, 08:49 PM
  2. Reading string from a file
    By kuma0177 in forum C Programming
    Replies: 1
    Last Post: 02-05-2010, 06:58 PM
  3. Hi Help with reading a file and string modification
    By doubty in forum C Programming
    Replies: 3
    Last Post: 08-06-2009, 02:11 AM
  4. Reading a string from file in quotes..
    By aker_y3k in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2003, 11:45 AM
  5. reading file into a string array
    By lambs4 in forum C Programming
    Replies: 7
    Last Post: 01-22-2002, 04:23 PM