Thread: C Help

  1. #31
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Well Ive been working on it with a friend from my class, and I want to try something that he started on last night. Yes it probably is something simple, but if we compare codes I think we may get somewhere. Thanks tho, Ill post again if I need further assistance

  2. #32
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Alright I think I may have gotten somewhere...I hope. I tried putting our 2 codes together, and came up with the following below. However I am getting a parse error before fgets, also I am getting an error about "the comparison of distinct pointer types lacks a cast" whatever that means.


    anyhow heres the code
    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    
    char filename[100];
    char arr[200];
    
    printf("Enter a FileName.");
    while (scanf<"%s",filename);
    
    { fopen(filename, "r")
    
    fgets(filename,200, stdin)
    
    fputs (filename, stdout);
    
    }
    
    
    }
    I hope Im not horrible with my loops heh.

  3. #33
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Alright seems like I have gotten the program to enter the loop, but there is no result. I think because it doesnt know where to exit the loop.

    Code:
    #include <stdio.h>
    
    int main(void)
    
    {
    
    char filename[100];
    char arr[200];
    
    printf("Enter a FileName.");
    while (scanf("&#37;s",filename));
    
    { fopen(filename, "r");
    
    fgets(filename,200, stdin);
    
    fputs (filename, stdout);
    
    }
    
    
    }

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do you actually want to read a filename each loop iteration, and then open that file without closing the previous one? Bad idea, I think. And I presume you actually want to read the entire file, not just one line? Perhaps you should do a loop around fgets() instead?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There's just so many problems with that code, I don't know where to begin...
    And your earlier code was even worse.

    Quote Originally Posted by CrazyShellsSlam View Post
    Code:
    #include <stdio.h>
    int main(void)
    {
    char filename[100];
    char arr[200];
    printf("Enter a FileName.");
    while (scanf<"%s",filename);
    { fopen(filename, "r")
    fgets(filename,200, stdin)
    fputs (filename, stdout);
    }
    }
    scanf is NOT a stream, so < doesn't work. It's a function, so you call use it using ().
    And doing a while loop with scanf as the condition is also a bad idea.
    scanf returns > 0 if the user enters anything and it puts it into your buffer. In other words, it will keep looping until you hit Ctrl+C or whatever the equalient on linux. Bad programming. Make the user enter something to quit the app, or better yet, make a menu. Ask if the user wants to quit or read another file.
    EVERY line needs a semicolon ; at the end, and you just forgot two of them in that code.
    fopen returns a pointer to the stream it created for the file, but you're disregarding it.
    fopen can also fail! Don't forget to check for error by checking if it returns NULL!
    You are trying to read 100 bytes from the keyboard into the buffer that contains your filename, which also is only 100 bytes, not 200. Bad, very bad.

    Quote Originally Posted by CrazyShellsSlam View Post
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    char filename[100];
    char arr[200];
    printf("Enter a FileName.");
    while (scanf("%s",filename));
    { fopen(filename, "r");
    fgets(filename,200, stdin);
    fputs (filename, stdout);
    }
    }
    This one is a little better, though.
    You still need a menu.
    Re-write your loop.
    Pass the stream returned from fopen to fgets, not stdin! You want to read from the file, not the keyboard!
    Read that contents into its own buffer, not the filename, and specify right buffer size!
    And the love of the gods, also close the files after you're done reading from them, please.

    Poor code. You need a little practice, I think. Good luck.

  6. #36
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    LOL Yeah I know. Thank you.

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, there were two other things I was going to add:
    If you call fgets if fopen fails (with your file pointer returned by fopen of course), the program will crash! Always make sure fopen didn't fail before you call fgets or fclose.
    Also work a little on your identing.

  8. #38
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Well I have already clarified that fopen works. You see with the present code I have the program compiles, and then runs. It asks me for a filename, and I give it one, and then I think its entering the loop, and doesn't know what to do with anything. I will make the corrections that you suggested. Thank you again.

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not talking about IF it works or not. I'm mentioning it's bad programming.
    If you do a program and send it to someone. This application does something with files. So the user inputs a file, your app tries to open it, but the user made a mistake in inputting it maybe, so your app can't open it, then crashes.
    Looks bad in the user's eyes, doesn't it?
    Sometimes you can't open a file because you don't have read/write permission or it doesn't exist or something else. It's bad programming style! Always check if fopen returns NULL. And print an error if it does instead of crashing!

  10. #40
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Quote Originally Posted by Elysia View Post
    If you call fgets if fopen fails (with your file pointer returned by fopen of course), the program will crash! Always make sure fopen didn't fail before you call fgets or fclose.
    good advise thanks

  11. #41
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Quote Originally Posted by Elysia View Post
    I'm not talking about IF it works or not. I'm mentioning it's bad programming.
    If you do a program and send it to someone. This application does something with files. So the user inputs a file, your app tries to open it, but the user made a mistake in inputting it maybe, so your app can't open it, then crashes.
    Looks bad in the user's eyes, doesn't it?
    Sometimes you can't open a file because you don't have read/write permission or it doesn't exist or something else. It's bad programming style! Always check if fopen returns NULL. And print an error if it does instead of crashing!
    Alright, thanks. so I guess I have to go back and tear the damned thing apart.

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not really. Just check for NULL after fopen, print and error and re-start the process. The keyword continue can restart the loop from the beginning.

  13. #43
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Well actually I did something a tad bit different that a professor from last semester said. if we want to check if something is actually happening, then we can use an "if statement" for instance you said to check if fopen returns null. Well I have it so that if the file is being opened then it will return the message that the file is opened or else return false. IS that an equally good idea?

  14. #44
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Actually nevermind, your right lol. Alright I need to work further on this

  15. #45
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Actually I got it...hehe with the if statement too. Heres the code.

    Code:
    #include <stdio.h>
    
    int main(void)
    
         {
    
    char filename[100];
    char arr[200];
    
    printf("Enter FileName /n");
    
    scanf("&#37;s",filename);
    
    if (fopen(filename, "r")) {
    printf ("The file is open.");
                             }
    else (printf("The file failed to open"));
    
    
        }
    Look good? Now I just need to get it to show the first line of the file...

Popular pages Recent additions subscribe to a feed