problem with reading from a file

This is a discussion on problem with reading from a file within the C Programming forums, part of the General Programming Boards category; heres the problem trying to read in a text file(sample below) and read these values into an array, i will ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    Smile problem with reading from a file

    heres the problem trying to read in a text file(sample below) and read these values into an array, i will use the array to check for valid users.

    n123456
    origam3
    k987654
    n290865

    heres the code

    Code:
    #include <stdio.h>
    
    main()
    {
      FILE *stream;
      stream = fopen("passwords1", "r");
      
      if (stream == NULL)
      {
        printf("File Not Loaded Correctly");
      }
      char linebuffer[10];
      char *users[2];
      int i =0;
        
      while(fgets(linebuffer, 10, stream))
      {
        sscanf(linebuffer,"%s", &users[i]);
        printf("%s\n", &users[i]);
        i++;
      }
        
      printf("\n");
      
      for(i=0;i != 4;i++)
      {
        printf("Value:%s\n",&users[i]);
      }
    }
    
    //output produced by first printf with the while loop
    n123456
    origam3
    k987654
    n290n29
    
    //output produced from second printf outside of the stream
    Value:n123orign290n290n29
    Value:orign290n290n29
    Value:n290n290n29
    Value:n290n29
    as u can see the code prints out correctly when accesing the first printf except fot the last one, but not when im accessing the one outside of the loop, this has been doing my head in for ages, any help much apreciated, cheers

    a1dutch

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       char *users[2];
    Where do you allocate memory for users?
    Code:
    sscanf(linebuffer,"%s", &users[i]);
    printf("%s\n", &users[i]);
    Why the &'s?

    [edit]And why use sscanf to scan a string into a string? The FAQ shows how to remove the trailing newline if that's all you're after.
    Last edited by Dave_Sinkula; 04-07-2005 at 09:57 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
      while(fgets(linebuffer, 10, stream))
      {
        sscanf(linebuffer,"%s", users[i]);
        printf("%s\n", users[i]);
        i++;
      }
    Try this code. I think you're reassigning to a pointer over and over again.

    You see, users[i] evaluates to *(users+i), so you needn't give sscanf or printf a reference to it. In other words, arrays are always passed by reference because they're basically pointers.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    i havent allocated any memory to
    Code:
     users[2]
    whats the easiest way to do this?

    without the & it wont print out!

    sorry if im a bit dumb, i learnt java and now moving to C, its a lot harder when dealing with strings!

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    joshdick i chnaged it to, it dosent even comppile now!

    Code:
     
      while(fgets(linebuffer, 10, stream))
      {
        sscanf(linebuffer,"%s", users[i]);
        printf("%s\n", users[i]);
        i++;
      }
    and it comes up with a compile error
    Code:
    clients.c:33: error: syntax error before "origam3"
    make: *** [clients] Error 1

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by a1dutch
    clients.c:33: error: syntax error before "origam3"
    It looks like your error is before "origam3." Look around there for your syntax error.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    dave i use sscanf as the original text file is user then whitespace(of no fixed length) then password

    n290865 password
    n456832 passwords
    n297236 password

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       FILE *file = fopen("passwords1", "r");
       if ( file != NULL )
       {
          char line[80];
          char user[5][10];
          size_t count, i = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%9s", user[i]) == 1 )
             {
                if ( ++i >= sizeof user / sizeof *user )
                {
                   break;
                }
             }
          }
          fclose(file);
          for ( count = i, i = 0; i < count; ++i )
          {
             printf("user[%d] = \"%s\"\n", (int)i, user[i]);
          }
       }
       return 0;
    }
    
    
    /* my output
    user[0] = "n290865"
    user[1] = "n456832"
    user[2] = "n297236"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    mate ur a legend, cheers. does sscanf require u to check the result is ok?

    e.g as in the code

    Code:
    if ( sscanf(line, "%9s", user[i]) == 1 )
    cos if i take it out of the if statement it dosent work?

    cheers for ur help

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >does sscanf require u to check the result is ok?

    It's best to get into the habit of using the return values to determine whether a function call was successful before continuing on.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    mate i tried changing that code u posted to read the password into another array,but it just aint having it, heres what ive put

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       FILE *file = fopen("passwords", "r");
       if ( file != NULL )
       {
          char line[80];
          char user[5][10];
          char userpw[5][10];
          
          size_t count, i = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%9s %s", user[i], userpw[i]) == 1 )
             {
                if ( ++i >= sizeof user / sizeof *user )
                {
                   break;
                }
             }
          }
          fclose(file);
          for ( count = i, i = 0; i < count; ++i )
          {
             printf("user[%d] = \"%s\",Password:%s\n", (int)i, user[i], userpw[i]);
          }
       }
       return 0;
    }
    also im gunna buy a C book do you recomend any??

    cheers

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if ( sscanf(line, "%9s %9s", user[i], userpw[i]) == 2 )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    There are countless threads in this board asking for book recommendations. The only C/C++ book I keep on my desk at work, though, is the C/C++ Programming Reference by Schildt. It's a very handy thing to keep around while programming. However, that's a reference book, not a teaching one, so search the board for those kinds of books.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by joshdick
    The only C/C++ book I keep on my desk at work, though, is the C/C++ Programming Reference by Schildt.
    You poor, poor fellow. Search the forum for schildt some time. He's one of the worst C/C++ authors of all time.

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

  15. #15
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I haven't run into any of the errors that others have pointed out, and I think he's a rather good writer. That book has always had the information I wanted.

    What C++ reference book do you use, praytell? And please don't tell me The C++ Programming Language...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Replies: 20
    Last Post: 06-13-2005, 12:53 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21