Thread: work with files

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    work with files

    I have a file named "file.txt" who looks like this
    " work.bmp
    12 14 15"
    I want to open file.txt, and than open the file named "work.bmp" in order to acces the BMP header.
    how do i do this?
    I tried like this:
    "FILE *image =fopen ("file.txt", "rt");
    fget(work.bmp, sizeof(work.bmp)+1, imagine);"
    but it doesn't work properly. can someone please explain me what i did wrong?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Next time, please post your code in [code][/code] tags. It should look like this:
    Code:
    FILE *image =fopen ("file.txt", "rt");
    fget(work.bmp, sizeof(work.bmp)+1, imagine);
    Some things I can see from the two lines you posted:

    • The function is fgets, not fget. More documentation in the man page if you need it.
    • You call the file pointer image, but in your fgets call, you use the name imagine.
    • You're trying to read into work.bmp, but it's not clear (because you didn't post enough code) whether that's a valid name of a struct and struct member, or you're confused on what exactly you're trying to do.


    Always remember, if you can't solve a problem yourself, you will never be able to program a computer to solve it. So first you must understand what the problem is asking, and how, in general, to do that problem. Start by reading up on the bitmap file format.

    Once you understand the problem and it's solution in general, you will want to begin writing out the steps your program needs to take, like directions you would find in a repair manual or baking recipe, etc. Something like:

    1. Open file.txt.
    2. Read the first line to determine the name of the bitmap file.
    3. Open that bitmap file (using a second FILE *). Note that bitmap files are binary, not text, so open in binary mode.
    4. Read the header info, using fread (which is used for reading binary info)
    5. ...


    Note that some of those are relatively easy: open a file with fopen, read a line with fgets, open another file with fopen. If you have a step that is more complicated or involved, you can break it into sub-steps.

    For example, reading the header may be broken down into
    4.a. Read the signature
    4.b Read the file size field
    4.c Skip the next X bytes
    4.d ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-18-2011, 08:51 PM
  2. work with files in С99
    By sanda in forum C Programming
    Replies: 4
    Last Post: 05-06-2010, 03:17 AM
  3. trying to work with 2 files at same time
    By morngrym in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2010, 10:59 AM
  4. How do headers and other c files work
    By noob in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 07:04 PM
  5. learning to work on files
    By cmay in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 12:50 PM

Tags for this Thread