Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:40:26+00:00 2026-06-09T01:40:26+00:00

So I’ve created a simple test application. It’s purpose is to either read or

  • 0

So I’ve created a simple test application. It’s purpose is to either read or write to a text file. It’s looking for the string “Firmware: “. The idea is that a device will have a package transferred to it and stored, then activated. I want to store the name of that package (Ex: Update1) and when I need to, query that and get the package name back (Read from the file).

Problem 1: It doesn’t seem to be writing to the file.
Problem 2: When I get it to write to the file, in the case in which the line “Firmware: pkgName” exists, how do I replace pkgName with the new package name?

This program runs and compiles, so feel free to throw it in an IDE or text editor, it just doesn’t fully do what I want. I am using log.txt as my sample file to read from. I’m sure there’s a more simple way to do this. I’ve been at this for some time and I’ve become stumped.

Thanks for the help!

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int writeFile(string filename, string pkgName);
int readFile(string filename);

int main(void) {
    string filename = "log.txt", userInput, pkgName;
    system("touch log.txt");

    while (true) {
        cout << "Write or Read?\n>>>";
        cin >> userInput;

        if (userInput == "Write") {
            cout << "What's your package name?\n>>>";
            cin >> pkgName;
            writeFile(filename, pkgName);
        } else if (userInput == "Read") {
            readFile(filename);
        } else {
            cout << "Invalid Input";
        }
    }
}

int writeFile(string filename, string pkgName) {
    bool found, exists = true;
    string line, word;
    fstream firmwareFile;
    int pos, size, num = 1;
    firmwareFile.open(filename.c_str(), ios::in | ios::out | ios::app);
    //firmwareFile << "This is a test.";
    if (firmwareFile.is_open()) {
        while (!found) {
            getline(firmwareFile, line);
            pos = line.find("Firmware: ");
            if (pos != string::npos) {
                //Found It
                found = true;
                exists = true;
                size = line.size();
                word = line.substr(10, ((size - pos) + 10));
                cout << word << " is the old package name" << endl;
                //TODO - Replace that with the new package name
                firmwareFile.close();
            } else if (firmwareFile.eof()) {
                //Doesn't Exist
                found = true;
                exists = false;
                cout << "No last firmware package available.\n";
            } else {
                //Still Looking
                found = false;
                cout << "Searching line #" << num <<  endl;
            }
            num++;
            if (!exists) {
                firmwareFile << endl << "Firmware: " << pkgName << endl;
                cout << "Wrote package name to file\n" << endl;
            }
            firmwareFile.close();
        }   
    } else {
        cout << "Unable to open file";
    }
}

int readFile(string filename) {
    bool found;
    string line, word;
    fstream firmwareFile;
    int pos, size, num = 1;
    firmwareFile.open(filename.c_str(), ios::in | ios::out | ios::app);
    if (firmwareFile.is_open()) {
        while (!found) {
            getline(firmwareFile, line);
            pos = line.find("Firmware: ");
            if (pos != string::npos) {
                found = true;
                size = line.size();
                word = line.substr(10, ((size - pos) + 10));
                cout << word << endl;
            } else if (firmwareFile.eof()) {
                found = true;
                cout << "No last firmware package available.\n";
                firmwareFile << endl << "Firmware: ";
            } else {
                cout << "Searching line #" << num << endl;
            }
            num++;
        }
        firmwareFile.close();
    } else {
        cout << "Unable to open file";
        firmwareFile.close();
    }
    firmwareFile.close();
}

Here is the contents of my log.txt file (Everything but the line containing “Firmware: ” is trivial)

You
Think
It's
Here
But
It's
Not
So
That
Sucks
Doesn't
It
?
Firmware: Update1
I
Hope
You
Caught
It
!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:40:28+00:00Added an answer on June 9, 2026 at 1:40 am

    You can simplify things if you write another file with only the relevant line changed:

    std::ifstream cfg("log.txt");
    std::ofstream tmp("log.tmp");
    std::string line;
    std::string pkgName;
    
    while(std::getline(cfg, line)) {
        if (line.find("Firmware:") != std::string::npos) {
            line = "Firmware: " + pkgName; 
        }
        tmp << line << '\n';
    }
    

    Now, renaming and deleting files is platform specific. If you want a portable solution, I suggest you look into Boost Filesystem library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.