Thread: Reading one line at a time...

  1. #1
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91

    Reading one line at a time...

    Well why do I always get "Couldn't open somefile.txt"?

    Code:
    #include <stdio.h>
    #define MAX_LINE_LENGTH 256
    
    int main(int argc, char *argv[])
    {
        if(argc != 2)
        {
             printf("Input should be of the form \'line filename.txt\'");
    	 return 1;
        }
    
        FILE *fp = fopen("argv[1]", "r");
        if(fp == NULL)
        {
            printf("Couldn't open %s", argv[1]);
    	return 1;
        }
        
        char line[MAX_LINE_LENGTH + 1];
    
        while(fgets(line, MAX_LINE_LENGTH, fp) != NULL)
        {
            printf(line);
        }
        return 0;
    }
    The file is there, so the problem isn't it...
    Last edited by lala123; 07-07-2005 at 03:01 PM.

  2. #2
    Griz803 Griz803's Avatar
    Join Date
    Jun 2004
    Posts
    5
    Hi, maybe you'd wanna try taking the double qoutes away from the expression argv[1] in the call to fopen. You don't want to open a file named argv[1] as a literal string, you want to open the value in argv[1] itself. Hope this helps.

  3. #3
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Oh thanks

    But now I have another problem...

    Code:
        while(fgets(line, 256, fp) != NULL)
        {
            if(strcmp(line, argv[2]) == 0)
            {
                printf("%s", line);
                fclose(fp);
                return 0;
            }
        }
    I wan't to compare the second argument argv[2] with the current line, but why do I always get no matches when they do match?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but why do I always get no matches when they do match?
    Because fgets takes the newline character with it. You can remove the newline any number of ways, but the common favorite is:
    Code:
    char *newline = strchr ( line, '\n' );
    
    if ( newline != NULL )
      *newline = '\0';
    However, I was always partial to this one because of its sheer cleverness:
    Code:
    line[strcspn ( line, "\n" )] = '\0';
    My best code is written with the delete key.

  5. #5
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thanks Works fine now.

  6. #6
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Hm... What's wrong with this line:

    Code:
    char *PrintMD5(UCHAR md5Digest[16]);
    I'm getting:

    Code:
    syntax error before "md5Digest"

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, what's before md5Digest? UCHAR perhaps? That isn't a keyword, so where might you find it defined?
    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.*

  8. #8
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Well I don't know... I hate using other people's code... Do you know a MD5 code that has no errors?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Do you know a MD5 code that has no errors?
    Or even a user who has a clue about porting code perhaps?

    Look at it again
    Then decide whether
    typedef unsigned char UCHAR;
    is perhaps the right thing to do
    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.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Quote Originally Posted by lala123
    Do you know a MD5 code that has no errors?
    check this site. Click the E-Security link in left pane. It has a link for MD5 code with explaination.

  11. #11
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Okay, I added:

    Code:
    typedef unsigned char UCHAR;
    typedef unsigned int UINT;
    But now I'm getting lots of undefined variables here:

    Code:
    static inline void FF(UINT& a, UINT b, UINT c, UINT d, UINT x, UINT s, UINT ac){ a += F(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
    static inline void GG(UINT& a, UINT b, UINT c, UINT d, UINT x, UINT s, UINT ac){ a += G(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
    static inline void HH(UINT& a, UINT b, UINT c, UINT d, UINT x, UINT s, UINT ac){ a += H(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }
    static inline void II(UINT& a, UINT b, UINT c, UINT d, UINT x, UINT s, UINT ac){ a += I(b, c, d) + x + ac; a = rotate_left(a, s); a += b; }

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you're compiling C++ code. Notice the keyword inline? Also notice the use of references (UINT& a, ...).


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

  13. #13
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Thanks. Got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. Segmentation Fault :(
    By DarkDot in forum C++ Programming
    Replies: 39
    Last Post: 04-07-2007, 04:16 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. line reading problem
    By papous in forum C Programming
    Replies: 2
    Last Post: 11-22-2001, 12:29 PM