C Help

This is a discussion on C Help within the C Programming forums, part of the General Programming Boards category; I need some help with a C program I am writing for my class. My professor wants us to write ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    38

    C Help

    I need some help with a C program I am writing for my class. My professor wants us to write a program that takes in filenames as args and returns the first line of each file.

    So far I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    {
    
    int 
    
    
    
    
    char filename[100];
    
    
    printf("Enter a file name.");
    scanf("%s",filename);
    
    
    
    
        
    
    
    
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    What's with that beginning "int"?
    Here's a question for you: do you know how to open/read files?

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Yeah, you use Fopen to open files

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    Do you know how to open and read files using C-style code? Otherwise you should read up a little ob the subject. There's excellent tutorials at the site.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Alright. Thanks u.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Alright so I did some poking around the site, which is funny cuz Ive been using this site for sometime as a reference, which has become a gold mine since Im a bit of a slow learner with languages anyhow. SO I have my program taking in a file name, it scans the file in background, and exits. Thats fine atleast the first part works.

    The second piece is getting it to the read the first line of a file and printing it to standard output. I looked on the site, but it says the page that Im looking for cannot be found. Its under C Tutorials its the FILE I/O. It seems to not be working so could someone please kindly put me in the right direction on the second piece of my program? Heres my updated code for reference, thanks.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    {
     
    
    char filename[100];
    
    
    printf("Enter a FileName.");
    scanf("%s",filename);
    
    
    
    
        
    
    
    
    }

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    You could read about fopen/fread/fclose. That should cover IO.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Well I went to the site. and I found the FILE I/O section of the C Tutorial but it gives me a 404 error. So if thats where the section abot FOPEN is then Im pretty screwed heh, unless theres somewhere else I can look

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The second piece is getting it to the read the first line of a file and printing it to standard output. I looked on the site, but it says the page that Im looking for cannot be found. Its under C Tutorials its the FILE I/O. It seems to not be working so could someone please kindly put me in the right direction on the second piece of my program?
    Wow, you're right. I'm going to let the webmaster know about this.

    Since the tutorial's down, here's what you'll need to do.
    1. Open the file with the name filename using fopen().
    2. Read the first line from the file with fgets().
    3. Close the file.
    4. Print the line that you read.

    You might be able to figure out how to do this with other tutorials, like this one: http://www.imada.sdu.dk/~svalle/cour...00000000000000
    But it's a bit hard to understand.

    You'd be better off looking at old threads, like this one:
    A File Pointer Question
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    You have a full reference over at msdn: http://msdn.microsoft.com/library

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Sure, there are lots of references around. I don't see a tutorial on MSDN, though. And a tutorial is what the OP needs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Thank you both for your assistance, Ill let u know if I get newhere

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    Perhaps. But perhaps the OP might also try to read over what the actual functions does. Good practice. When doing real programming, you don't find a lot of tutorials saying how you do X and Y, you refer to your documentation.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    38
    Well yeah. Thats what Im doing. Only thing is my text book, and excuse my language, sucks at that, lol. I mean it explains the terminology, but whats good terminology without good examples? You see thats my problem, IM a visual learner. I cannot learn by simply reading, I learn by doing. SO if I were to see an example, and plug it into my console and see it in action, I would better understand things than just reading about it.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,439
    And that's why it's a good thing there are examples for a lot of functions in docs!

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

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