Reading and Printing a text file

This is a discussion on Reading and Printing a text file within the C Programming forums, part of the General Programming Boards category; Okay, I want my program to read a text file and print it, but it doesn't seem to do that. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Reading and Printing a text file

    Okay, I want my program to read a text file and print it, but it doesn't seem to do that. Instead, it prints the first letter, which I type in, when it asks for the file name. A little help please?

    I know it's a simple problem, but I just recently started and would appreciate some advice, thanks!

    Coding deleted
    Last edited by Mikahcho; 12-06-2010 at 07:37 AM.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Mikacho from JPM forums?

    Code:
    ..}
    You were capturing a single character, this declares an array of characters.

    PS, if you want to deal with files you should learn how to open a file for reading and print the file contents to stdout.
    Last edited by tenchu; 12-06-2010 at 07:38 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    ngfjmhfm,dm,dg
    Last edited by Mikahcho; 12-06-2010 at 07:37 AM.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Haha, on JPM I am Google. Or, The Yellow Brick Road when I wasn't banned.


    Confused by what you wanted, feels like you're trying to do two things at once.

    edit:

    You should probably close the file before exiting as well, with fclose(fp);
    Last edited by tenchu; 12-06-2010 at 07:47 AM.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    scanf("%s", &key);

    Both of you, tenchu and Mikahcho, should note that key is not the same type as &key. According to the manual, scanf requires a pointer to the type of storage indicated by each conversion specifier in the format string. key, as a string, is already converted to a pointer to char when you use it in this context.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    You were Yellow Brick Road??! Dude, I loved you back then... You were so witty, why'd you get banned? ;-;

    Sorry if my request wasn't clear...

    What I need is a program which asks the user to input a file location. The program finds the text file, and reads it, character by character, and prints it on screen. That's it ;_;

    Okay, my friend just told me that I should be using a while statement and the fgetc function... So I might try to play around with that... And I'll try the code you wrote out, THANK YOU!

    @Whiteflags

    I didn't understand what you said... Eurgh, programming is the bain of my life haha

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    What you want is the answer I posted over in this other topic -> Reading a proc file in user space

    Except you want the user to enter the file location/name and not hard code it. You have already done that part, now you just need to combine that with with the other example.

    I was banned because the admin didn't like my wittiness. =(

    Whiteflags, so what you're saying is:

    name[] = string
    name = address of the array

    Scanf requires the address and therefore the & was redundant?

    I left the & in because it was part of his original code, I just slapped the [20] on the other key so I could capture the "%s" I didn't think to remove the &

    ~_~
    Last edited by tenchu; 12-06-2010 at 07:47 AM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    >> I didn't understand what you said... Eurgh, programming is the bain of my life haha

    What you're doing is wrong, so stop.

    You've declared in your program that key is

    char key[20];

    and you use it like

    scanf("%s", &key);

    The address of operator gets the address of the array. This works out to the type char (*)[20] meaning a pointer to array. The address of operator is one of the few contexts where arrays are not condensed to pointers to the zeroth element. It would be wrong even if that did happen, but the point is scanf is not built with pointers to arrays in mind.

    Like with other functions, scanf can change your string with a pointer to the zeroth element. The identifier key becomes such a pointer in many contexts, and this is one of those. You could also write anything equivalent to what scanf actually needs. I hope this is all clear.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    jtdjyrfjyrdjyd
    Last edited by Mikahcho; 12-06-2010 at 07:37 AM.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    edit: deleted double post in case you missed it.

    It works for me on Linux. What are you entering for the filename? You may not need to add the directory if it's in the same directory as the executable.

    It was son goku, haha.
    Last edited by tenchu; 12-06-2010 at 07:39 AM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Is it possible to pm on this forum? It seems I can't do it...

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    I don't see a PM yet, maybe I need more posts or maybe I am blind haha.

    If you ever want to PM me you can PM me on JPM.
    Last edited by tenchu; 12-06-2010 at 07:50 AM.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    fp=fopen("%s\n", "w");

    This will try to open a file for writing named "%s
    " which I doubt will work.

    Code:
    FILE *fp;
    fp=fopen("%s\n", "w");
    printf("%s\n", &key);
    
    char c;
    while (c != NULL){
        c = fgetc(fp);
        printf("%s", &key);
      }
    And you're still doing this. I posted a more complete reply before you posted this code, so please stop using &key after you read it. It doesn't work for printf either.

    Be aware that key will never change in that display loop. Based on what you've described, you could do it like this: edit tenchu's given example to fill up the array and then print the array whenever the array is full. Repeat this until the file is empty. Remember, you're working with strings!

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Read the pm I sent you please...
    Last edited by Mikahcho; 12-06-2010 at 07:42 AM.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    I removed quoted code.

    I think strings are really tricky. They are one of my most hated things to deal with. It takes some getting used to.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a file and printing out a graph from the data
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 07:32 PM
  2. printing arrays, reading from text file
    By joeman in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2010, 02:08 PM
  3. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  4. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 12:29 PM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM

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