Thread: something is squiffy

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    something is squiffy

    i have the following
    Code:
    int main()
    {
        //declare variables
        char buffer[1024] = {'\0'};
        FILE *pRead = NULL;
    
        pRead = fopen("names.txt", "r");
    
        if ( pRead == NULL )
        {
            printf("file cant be opened!!\n");
            return 0;
        }
    
        //fscanf( pRead, "%s", buffer );
    
        //fread(buffer, sizeof buffer, sizeof(char), pRead);
        while ( (fscanf(pRead, "\"%[^\"]\",", buffer) != EOF) )
        //while (fread(buffer, sizeof buffer, sizeof(char), pRead) != 1)
        {
    
            printf("%s\n",buffer);
        }
        fclose( pRead );
    
        return 0;
    }
    the only one that did something other than a load of empty lines in a infinite loop was but it would print the text file twice before ending if it was inside the while loop or print the text file in a infinite loop outside the while loop.

    if i make the buffer smaller it just throws a seg fault when it tries to read the file.

    i even copied the current while loop from the official answer and it just produces empty buffer again in an infinite loop

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    Well what's in your text file?

    Because if the first character isn't a ", your fscanf will just cause an infinite loop.

    This is how you read a text file.
    Code:
    while ( fgets(buffer, sizeof(buffer), pRead) ) {
      char quoted[1024];
      if ( sscanf(buffer, "\"%[^\"]\",", quoted) == 1 ) {
        printf("%s\n",quoted);
      } else {
        printf("Buffer doesn't match expected format\n");
      }
    }
    > i even copied the current while loop from the official answer
    You keep saying "official answer" like such a thing exists - it doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    if your libc supports ISO 27431-2:2010 (__STDC_WANT_LIB_EXT2__), then you can use getline() function which will allocate the buffer dynamically.

    Most glibc supports this.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the text file is here https://projecteuler.net/project/res...p042_words.txt it is a csv file with words in quotation marks ie "a","above","below"...
    i am twisting myself into knots here. All i am trying to do is read in one word at a time and when it gets to the end of the file quit

    im getting frustrated with myself because what ever combination of buffers and arrays unless i have stupidly large arrays it all falls over

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    Code:
    $ wc p042_words.txt 
        0     1 16345 p042_words.txt
    It's just ONE line of 16K characters.

    What are you trying to do?

    Print each word?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    yes or the entire thing and then split it myself

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,669
    The trick would seem to be dealing with the lack of a comma after the last word.

    So maybe something like
    Code:
    while ( (fscanf(pRead, "\"%[^\"]\"%*c", buffer) == 1 )
    The %*c will eat a comma (or indeed any other character), if there happens to be one.

    But it won't cause a scan failure at the end of input because it's missing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    CSV format standard (RFC 4180) says these are valid and are the same thing:

    "aaa","bbb","ccc"<crlf>
    aaa","bbb","ccc<crlf>

    And the " char is done by double quotes:

    10
    ,"abc""d","fred""<crlf>

    The second field here is abc"d. The third is fred" (the final ", closing the string is missing. "" is the character ").

    PS: Every line are always terminated by "\r\n", otherwise this isn't a CSV file. There's an exception: The last line may not be terminated by this final "\r\n".

    Spaces are part of the fields and cannot be ignored, the line below:

    aaa,bbb ,ccc<crlf>

    Have 3 fields: "aaa", "bbb " and "ccc".


    Last edited by flp1969; 05-25-2023 at 08:45 AM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread