Thread: Using C program to append text to txt file

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    5

    Using C program to append text to txt file

    Hallo

    I am new at C so please forgive me for the simple question. I am trying to understand how to append some code to a text file. I have run a simple program like the one below. Basically it opens a text file and then it appends the string aaaaaaaaaa
    Code:
    #include <stdio.h>
    #include <io.h>
    #include <stdlib.h>
    char s[10] = {"\naaaaaaaaaa"};
    int main(void)
    {
    FILE *file = fopen("test.txt", "a");
    fputs("\nHallo World",file);
    fputs(s,file);
    
    
    fclose(file);
    
    system("PAUSE");
    return 0;
    }
    Now that I know how to append the above string, I now would like to extend the experiment to append the following snipped of code in my test.txt file:

    Code:
    <Placemark>
    <name>12:01</name>
    <Snippet maxLines="0"></Snippet>
    <description>&amp;nbsp;</description>
    <styleUrl>#gv_waypoint</styleUrl>
    <Point>
    <coordinates>30,-29,0</coordinates>
    </Point>
    </Placemark>
    How do I go about doing this? The problem I am experiencing is that all the / and " characters in the above snippet of code seems to get the C compiler confused. I have limited knowledge on this subject and I would appreciate some guidence.

    One thought I had was to create a string like the one below but as I mentioned, the number 0 in the code is surrpunded by " " and this confuses the compiler:
    Code:
    char s[250] = {"<Placemark>
    <name>12:01</name>
    <Snippet maxLines="0"></Snippet>
    <description>&amp;nbsp;</description>
    <styleUrl>#gv_waypoint</styleUrl>
    <Point>
    <coordinates>30,-29,0</coordinates>
    </Point>
    </Placemark>"} ;

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    To have quote characters in a string within source code, escape each one with a backslash. Backslashes also have to be escaped (with a second backslash).
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("\"Hello\"\n");   /*   will output Hello surrounded by double quotes, then a newline  */
    }
    Probably better to put the text you want to append in a separate file, and then read data from that file to append it to the other. No need to use a backslash to escape things if the data is read by the program from a file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Since strings are denoted by "quotation marks" you need to escape them if they are part of your data. You escape any character with \, like '\"'. If you intend to put in a \ you must write '\\'. You generally don't have to worry about '/' characters. So that is pretty much the solution here.

    You can also use syntax highlighting to help yourself.
    Code:
    "<Placemark> \
    <name>12:01</name> \
    <Snippet maxLines=\"0\"></Snippet> \
    <description>&amp;nbsp;</description> \
    <styleUrl>#gv_waypoint</styleUrl> \
    <Point> \
    <coordinates>30,-29,0</coordinates> \
    </Point> \
    </Placemark>"
    Last edited by whiteflags; 03-16-2013 at 04:09 AM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char s[10] = {"\naaaaaaaaaa"};
    Are you aware that your array is too small? You have 10 a's, one newline and one '\0' (for terminating a C-String) thus you would need an array which can at least hold 12 characters.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. append to text file
    By ulti-killer in forum C Programming
    Replies: 4
    Last Post: 10-13-2012, 12:52 PM
  2. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  3. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 01:01 AM
  4. Append to a file
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2005, 02:12 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 08:37 AM