Thread: Garbage values in File

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    13

    Unhappy Garbage values in File

    Hello..

    I'm creating a text file in C. I'm able to read and write into it correctly. However, when i open the file in Notepad or in command prompt edit, it shows garbage values.

    What is the reason for this? And is there a way to read the contents of the file without my program ?


    If this mite be a reason..I'm writing records as structures into the file using fwrite..


    Thanks..

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    If you write a structure, you have to be careful. The data will be written as binary, so you have to open the file as binary. Also, writing pointers to files make no sense. Finally, integers are written in binary. That is, the integer 267 may be written to the file as 4 bytes: "\x02\x01\x00\x00". Those bytes won't be human readable. If you want "267" written to the file, you'll have to convert it yourself to a string and write that to a file.

    Really, you don't seem to be understanding how data is stored and/or written to a file. I suggest you don't directly write a structure to a file. First of all it's not portable; second of all you'll need to be able to understand what it does before you use it, which apparently you don't.

    Just convert the structure to a string that you want, then write it to a file. When reading from the file in your program, convert it back to the structure manually.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Ha, I just posted an example of writing plain text to file here which might be helpful:
    FILE and strtok problems (C Linux)
    Last edited by HowardL; 04-11-2010 at 09:24 AM.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    thanks..i'll consider what u say but as of now i'm short of time..

    its like im creating a student database. i accept the name,roll no.,marks etc as the variables in the structure and then write the whole structure to the file..

    what is the alternate way to do this?
    As in how else can i write it such that it is in human legible text form..?
    Last edited by wayne08; 04-11-2010 at 09:28 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Use fprintf() to write out each of the struct members to the file.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    13
    cool..i'll try..thanks..
    Last edited by wayne08; 04-11-2010 at 11:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM