Thread: Reading and appending text files

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    3

    Question Reading and appending text files

    So I am starting to create a program that will be a multi step process. The principle is that the programming I am creating is to help our field engineers quickly add some lines to an text file that our main program uses for settings. I want to take the text file and append it to a new file until the line <settings> is found. From there I will do another step to append the new lines need then continue, but I am stuck on getting it to go into the array up to that line and to be honest I am not sure where to begin. I've been looking through the net but brain is now mush. Any advice, help, suggestions or examples would be appreciated.

    Thanks,
    Vendetto

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    39
    Let me try to explain in simple words:
    Code:
    1. open a stream for reading
    2. open a stream for output
        while reading a line from input
           if line doesn't start with <settings> then   
              write line to output.
           end-if
        end-while
    3. now append the new stuff to output
    4. while reading a line from input
           write line to output
        end-while
    If you don't know about std::ifstream and std:fstream have a look at https://www.learncpp.com/cpp-tutorial/186-basic-file-io/

    Hope this gives you an idea how to start

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    765
    Start? Where is YOUR code?

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    3
    Thanks thmm!!!

    That did help me get started. Here is where I am so far:

    Code:
    //
    
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <Windows.h>
    
    int main()
    {
        int results;
        char oldname[] = "c:\\synergyii\\config\\clientconfig.xml";
        char newname[] = "c:\\synergyii\\config\\clientconfig.txt";
        results = rename(oldname, newname);
        if (results == 0)
            puts("File successfully renamed");
        else
            perror("Error renaming file");
    
    
        
        std::ifstream inf{ "c:\\synergyii\\config\\clientconfig.txt" };
    
    
        std::fstream outf{ "c:\\synergyii\\config\\clientconfig.xml" };
    
    
        if (!inf)
        {
            std::cerr << "Cannot open client config for reading" << std::endl;
            return 1;
        }
    
        if (!outf)
        {
            std::cerr << "Cannot open client config for writing" << std::endl;
            return 1;
        }
    
        while (inf)
        {
    
            std::string line;
    line.assign("</settings>");
    
    
            std::string strInput;
            std::getline(inf, strInput);
    
    
            if (strInput !=  line)
            {
                outf << inf.rdbuf();
            }
    
        }
    
        return 0;
    
    
    }

    I am running into 2 problems.

    1. The first line of the text document is being cut off when it runs. Everything else copies.
    2. The if statement is not working. It is still reading and appending to the end of the file.

    I know there isn't much here yet but I am building on it. I think I need to have an else statement in there but not sure. I just want it to get to the line that is </settings> and stop the reading of that inf and close it. Then I will open another and do the process again and so forth until a new xml is built.

    Like I said, any help or advice is appreciated.

    Thanks,
    Vendetto

  5. #5
    Registered User
    Join Date
    Nov 2020
    Posts
    3

    Question

    Ok so the programming is working great. I got around all my issues. However, I am now looking for a way for the program to check the lines in "newline.txt" and see if they are duplicates of the ones in the output file and if so, do not copy them. Any suggestions?

    Code:
    
    //
    
    
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <Windows.h>
    
    
    int main()
    {
        int results;
        char oldname[] = "c:\\synergyii\\config\\clientconfig.xml";
        char newname[] = "c:\\synergyii\\config\\clientconfig.txt";
        results = rename(oldname, newname);
        if (results == 0)
            puts("File successfully renamed");
        else
            perror("Error renaming file");
    
    
        
        std::ifstream inf{ "c:\\synergyii\\config\\clientconfig.txt" };
    
    
        std::ofstream outf{ "c:\\synergyii\\config\\clientconfig.xml" };
    
    
        if (!inf)
        {
            std::cerr << "Cannot open client config for reading" << std::endl;
            return 1;
        }
    
    
        if (!outf)
        {
            std::cerr << "Cannot open client config for writing" << std::endl;
            return 1;
        }
    
    
        std::string line{ "</settings>" };
        std::string strInput;
    
    
        while (std::getline(inf, strInput))
        {
            if (strInput == line)
            {
                break;
            }
       
            else 
            {
                outf << strInput << '\n';
            }
    
    
        }
    
    
        inf.close();
    
    
        std::ifstream inf2{ "c:\\synergyii\\config\\newlines.txt" };
    
    
        if (!inf2)
        {
            std::cerr << "Cannot open client config for reading" << std::endl;
            return 1;
        }
    
    
        while (std::getline(inf2, strInput))
        {
            outf << strInput << '\n';
        }
    
    
        inf2.close();
    
    
        outf << "</settings>" << '\n';
        outf << "</config>" << '\n';
    
    
        outf.close();
    
    
        return 0;
    }
    Thanks,
    V

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading files from text and looking for text
    By november1992 in forum C Programming
    Replies: 18
    Last Post: 04-30-2012, 02:18 PM
  2. Reading text files
    By Xinco in forum C++ Programming
    Replies: 20
    Last Post: 01-09-2007, 01:24 PM
  3. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  4. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  5. appending to/reading from text file
    By BR7 in forum C Programming
    Replies: 5
    Last Post: 02-22-2002, 05:02 PM

Tags for this Thread