Like Tree1Likes
  • 1 Post By whiteflags

fread Problem - Random Characters Printing

This is a discussion on fread Problem - Random Characters Printing within the C Programming forums, part of the General Programming Boards category; (SOLVED!) Hey there. I've been having this issue for awhile, and its not too bad. Its just, whenever I use ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    fread Problem - Random Characters Printing

    (SOLVED!)

    Hey there. I've been having this issue for awhile, and its not too bad. Its just, whenever I use fread to read from a text file to print to the screen, it also prints random characters that aren't suppose to be there. I've tried looking on Google but couldn't find an answer. Well, I did find answers but none of them worked for me.

    Code:
    FILE *title;
    char titlestr[713];
    
    title = fopen(".//text//title.txt", "r");
    fread(titlestr, sizeof(titlestr), 1, title);
    printf("%s", titlestr);
    That's my current code; why are random characters being printed?
    Last edited by Coool Steve; 10-03-2011 at 03:27 PM.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    What do you want to be printed?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    The file title.txt. Its printing correctly, its just random characters like ☺, ♦ and other characters like that are being printed at the end, also.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fread isn't typically your best choice for text mode. You probably want fgets or maybe fscanf. That isn't to say you can't use it, but you should be checking its return value, and you should be making sure that is a nul terminated bunch of text if you plan on using it as a string. Typically a .txt file isn't going to include a nul terminator.


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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Well, I did try fscanf, but I could never figure out to make it read more than one line.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Use a loop.
    quzah likes this.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Coool Steve View Post
    Hey there. I've been having this issue for awhile, and its not too bad. Its just, whenever I use fread to read from a text file to print to the screen, it also prints random characters that aren't suppose to be there. I've tried looking on Google but couldn't find an answer. Well, I did find answers but none of them worked for me.

    Code:
    FILE *title;
    char titlestr[713];
    
    title = fopen(".//text//title.txt", "r");
    fread(titlestr, sizeof(titlestr), 1, title);
    printf("%s", titlestr);
    Fist off... sizeof(titlestr) is extremely unreliable. You should define a constant and use that instead...
    Code:
    #define TSTRLEN 713
    
    fread(titlestr, TSTRLEN,1, title);
    But this only works if titlestr is *always exactly* 713 characters long... In any other case you will either get only part of the string or you'll end up reading past the end of the string into whatever else is in the file.

    That's my current code; why are random characters being printed?
    Because the string is not null terminated... Of that 713 characters you need to know how many are actually used... before you can print it.

    Here's a trick you can use...

    Code:
    #define TSTRLEN 713
    
    FILE *title;
    char titlestr[TSTRLEN];
    
    title = fopen(".//text//title.txt", "r");
    memset(title,0,TSTRLEN);
    fread(titlestr,1, TSTRLEN, title);
    printf("%s", titlestr);
    Although I think you will find fgets() would be a far better way to read that string from disk.
    Last edited by CommonTater; 10-03-2011 at 03:15 PM.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Tried using the example you gave me, unfortunately, whenever I use it it just prints a single "-", nothing more. I'll try fgets instead.

    Thanks to all of you for your assistance! fgets has proven to be a better choice. I was really surprised at how fast my question was answered!

    (Especially you, CommonTater!)
    Last edited by Coool Steve; 10-03-2011 at 03:32 PM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... just happened to be in the neighborhood.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    No worries... just happened to be in the neighborhood.
    He's like a hobo, and we're all out of boxcars.


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

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by Coool Steve View Post
    Code:
    title = fopen(".//text//title.txt", "r");
    You dont need this double slashes, they are wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem printing ASCII control characters
    By d2rover in forum C Programming
    Replies: 12
    Last Post: 10-14-2010, 07:46 AM
  2. fread + non-english characters
    By Elysia in forum C++ Programming
    Replies: 25
    Last Post: 04-20-2010, 01:43 PM
  3. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  4. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  5. Printing different representations of characters...
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 03-04-2006, 12:05 PM

Tags for this Thread


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