Thread: Opening a file and formatting the output?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Opening a file and formatting the output?

    Let's say that I have a program that, so far, opens a TXT file and prints it back to the user.

    Code:
    #include <stdio.h>
    
    void main()
    {
    
       FILE *fopen(), *fp;
       int c;
       fp = fopen("text.txt","r+");
       c = getc(fp) ;
       while (c!= EOF)
       {
       		putchar(c);
    		c = getc(fp);
       }
       fclose(fp);
    }
    I would like output of the read file to be formatted depending on what is read. For example, if 0x01 (Hex Value 01, yes) is read in the file, that is converted to \n (New Line function) and the final display to the user would be a new line.

    Example (text.txt):
    "How are you today, Tom?" asked Tim.[0x01 Hex Character here.]"I'm well, Tim. Thanks. How are you, Tim?" asked Tom.

    Would be displayed as:
    "How are you today, Tom?" asked Tim.
    "I'm well, Tim. Thanks. How are you, Tim?" asked Tom.

    I look forward to any assistance I can get. Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why use 0x01 for the newline why not use the newline char (0x0a)?

    Jim

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... why wouldn't you just put a newline in the file?

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    40
    Placing \n in the .txt file results in it being printed instead of a new line being made. Also, I forgot to mention... this is going to be used as a text parser for an existing video game that uses 0x01 for "New Line," 0x0F to show a certain variable inputted by the player, etc. Parsing it into something more user friendly and displaying it in the Command Line window is my plan.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Flotonic View Post
    Placing \n in the .txt file results in it being printed instead of a new line being made.
    That's because '\n' is different than '\' + 'n'.


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

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ok, then when you parse the file and run into the 0x01 convert it to the newline character to display it in the command line window.

    Jim

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    40
    Quote Originally Posted by jimblumberg View Post
    Ok, then when you parse the file and run into the 0x01 convert it to the newline character to display it in the command line window.

    Jim
    That's pretty much the question, though. How would I go about searching for that in the file before parsing it and making it be converted? I've tried giving the #define thing a shot as well, but it didn't take me anywhere.

    #define 0x01 \n

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Flotonic View Post
    That's pretty much the question, though. How would I go about searching for that in the file before parsing it and making it be converted? I've tried giving the #define thing a shot as well, but it didn't take me anywhere.

    #define 0x01 \n
    You can't define a number as something else. Also \n is not the same thing s '\n'.


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

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the following code you are reading the file character by character.
    Code:
       c = getc(fp) ;
       while (c!= EOF)
       {
       		putchar(c);
    		c = getc(fp);
       }
    If you test for 0x01 before you write the value to your console with putchar(c) and if it is 0x01 then write 0x0A instead of the 0x01 it should work. You may want to look at this link.

    Jim

  10. #10
    Member
    Join Date
    Mar 2011
    Posts
    40
    Quzah:
    That's very helpful. #define is for more like labeling variables, in a way. I've seen the syntax before. Thanks.

    Jimblumberg:
    When you say, "testing for it," I get a slight idea in the back of my head. Do you have any example of that for me to burn into my mind? Thanks for the help.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Flotonic View Post
    Quzah:
    That's very helpful. #define is for more like labeling variables, in a way. I've seen the syntax before. Thanks.
    I know what a #define is. I also know what 0x01 is. 0x01 is a number. You can't have variables named after numbers, and you can't redefine numbers as something else. You can't do this either:
    Code:
    #define 4 5
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Member
    Join Date
    Mar 2011
    Posts
    40
    Quote Originally Posted by quzah View Post
    I know what a #define is. I also know what 0x01 is. 0x01 is a number. You can't have variables named after numbers, and you can't redefine numbers as something else. You can't do this either:
    Code:
    #define 4 5
    Quzah.
    Yes. A #define can be used for something more like:

    Code:
    #include <stdio.h>
    
    #define MUFFINS 15
    int main()
    {
    printf("You have %i muffins.\n",MUFFINS);
    printf("Your friend gave you thirty new ones.\n");
    printf("You now have %i muffins.\n",MUFFINS+30);
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What's your point? Go on, compile what you wrote earlier:
    Quote Originally Posted by Flotonic View Post
    #define 0x01 \n
    I already told you I know what a #define is. You can't do what you wrote in this quote here. You aren't allowed. You cannot define a number, which is what 0x01 is, as something else. I don't know how many times I need to repeat myself here.


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

  14. #14
    Member
    Join Date
    Mar 2011
    Posts
    40
    If you actually read my original post carefully, you would have seen, "I've tried giving the #define thing a shot as well, but it didn't take me anywhere."

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I did read it, and I told you that you can't do what you tried. Then you tried to convince me that you knew what you were doing, even though you didn't.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting output to a file.
    By arupsarkar in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2010, 07:25 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  5. formatting output in C++
    By ubershatten in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 10:15 AM

Tags for this Thread