Thread: fgets issue reading in first 34 characters of a line.

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    15

    fgets issue reading in first 34 characters of a line.

    Code:
        // - a function that will read in 1 input file
        void inputCSV(int year) {
    
    
            char file_name_start[] = "yob";
            char file_name_end[] = ".txt";
            char buffer_file_name[20];
            char buffer_input_name[MAX_NAME_LENGTH];
    
    
            sprintf ( buffer_file_name, "%s%d%s",
                    file_name_start, year, file_name_end);
    
    
            printf ("\n%s", buffer_file_name);
    
    
            FILE *csv_file = fopen(buffer_file_name, "r");
            printf ("\n%s", buffer_input_name);
    
    
           fgets ( buffer_input_name, sizeof(buffer_input_name-1), csv_file);
    
    
            printf ("\n%s", buffer_input_name);
            fclose(csv_file);
        }
    
    
    
    
        //- a function that will read all 10 input files
        void inputFiles(int *ptr_year){
    
    
            do {
    
    
                inputCSV(*ptr_year);
                (*ptr_year)+=10;
    
    
            } while ( *ptr_year <= 2010);
    
    
    
    
    
    
        }
    int input starts at 1910.
    MAX_NAME_LENGTH = 35 characters

    I am trying to essentially increment the file and save the first 35 characters to a temp string, I understand it will only actually hold 35-1 items due to the terminator.

    I am required to pass this string to another function to process the data and pull out the first csv item. I must do so in this manner due to assignment requirements.

    currently the program runs and does not print anything unless I comment out the fgets.

    if someone would be so kind as to point out what I'm doing wrong that would be awesome.

    also, I am passing the int pointer to keep track of the current year being process. file name, file csv format is defined as static and will not change from file to file, except for individual names and numbers.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I have no idea if this is legal in C

    Code:
    sizeof(buffer_input_name-1)
    I think you likely meant to use
    Code:
    sizeof(buffer_input_name)-1
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    I tried that, and have left it that way but it is not currently working still. The error causes everything to stop printing. When I comment out fgets it then prints the output, leaving what I guess is a null character printed for the buffer.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    It's looking like fgets will do one less than the assigned size anyways, so I can remove that -1, unless I'm incorrect about that.

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    looking like it isn't opening the file.

    does anyone have a link or suggestion for how to properly check that the file is valid and opened?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by bizkit86 View Post
    looking like it isn't opening the file.

    does anyone have a link or suggestion for how to properly check that the file is valid and opened?
    Code:
    yourfilepointer = fopen(yourfilename, "r");
    if (yourfilepointer == NULL) {
       perror(yourfilename); /* see what went wrong */
       /* exit this function, write to an log, try another file, whatever */
    }

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    Code:
    //defines the maximum length of a word (assumes maximum is 35 character)
        #define MAX_NAME_LENGTH 35
        //defines the maximum number of names (total number of possible names)
        #define MAX_NUMBER_OF_NAMES 1000
        //DEFINES THE MAXIMUM NUMBER OF INT POSITIONS (2010-1910 / 10yrs)
        #define MAX_NUM_YEARS 10
    
    
    
    
        // - a function that will read in 1 input file
        void inputCSV(int year) {
    
    
            char file_name_start[] = "yob";
            char file_name_end[] = ".txt";
            char buffer_file_name[20];
            char buffer_input_name[1024];
            //char c;
            int i = 0;
    
    
            sprintf ( buffer_file_name, "%s%d%s",
                    file_name_start, year, file_name_end);
    
    
            printf ("\n%s", buffer_file_name);
    
    
            FILE *csv_file = fopen(buffer_file_name, "r");
            printf ("\n%s", buffer_input_name);
    
    
           /* if (csv_file) {
               while ((c = getc(csv_file)) != EOF)
                    putchar(c);
                fclose(csv_file);
            }*/
    
    
    
    
            for (i = 0; i < 100; i++) {
                fgets (buffer_input_name, sizeof(buffer_input_name), csv_file);
                printf ("\n%s", buffer_input_name);
            }
    
    
            fclose(csv_file);
        }
    
    
    
    
        //- a function that will read all 10 input files
        void inputFiles(int *ptr_year){
    
    
            do {
    
    
                inputCSV(*ptr_year);
                (*ptr_year)+=10;
    
    
            } while ( *ptr_year <= 2010);
    
    
    
    
    
    
        }

    looks like it is working when I run it in cygwin. When I run it through eclipse I get a blank screen.

    so it looks like I can move on to the next part of my assignment.

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    thanks, I will update my code with that. Is perror cross platform?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes it is standard, look here.

  10. #10
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    Thank you very much!

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    Awesome, works great so far. Thanks guys for taking the time to read my post and help.

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by bizkit86 View Post
    Is perror cross platform?
    You can find out that and more at the Linux man-pages project. Just look at the Conforming to section in any function you're interested in. C89 is universally available, C99 and POSIX everywhere except Windows, and so on. It is an excellent resource, and actively maintained.

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    15
    bookmarked, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets to read a line without returning the new line char \n
    By khaliiid1992 in forum C Programming
    Replies: 7
    Last Post: 11-06-2014, 02:53 PM
  2. How to prevent fgets from reading the new line character
    By Nyah Check in forum C Programming
    Replies: 2
    Last Post: 03-12-2013, 01:59 PM
  3. Possible fgets issue
    By Layvian in forum C Programming
    Replies: 7
    Last Post: 05-22-2012, 11:32 PM
  4. reading line by from file without fgets
    By gioullis in forum C Programming
    Replies: 1
    Last Post: 12-21-2011, 09:58 AM
  5. fgets reading the same line twice
    By lukeaar in forum C Programming
    Replies: 3
    Last Post: 03-25-2010, 06:50 AM

Tags for this Thread