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 6696985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:24:22+00:00 2026-05-26T06:24:22+00:00

I wrote the code below that successfully gets a random line from a file;

  • 0

I wrote the code below that successfully gets a random line from a file; however, I need to be able to modify one of the lines, so I need to be able to get the line character by character.
How can I change my code to do this?

  • 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-05-26T06:24:22+00:00Added an answer on May 26, 2026 at 6:24 am

    Use std::istream::get instead of std::getline. Just read your string character by character until you reach \n, EOF or other errors. I also recommend you read the full std::istream reference.

    Good luck with your homework!

    UPDATE:

    OK, I don’t think an example will hurt. Here is how I’d do it if I were you:

    #include <string>
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    static std::string
    answer (const string & question)
    {
        std::string answer;
        const string filename = "answerfile.txt";
        ifstream file (filename.c_str ());
    
        if (!file)
        {
            cerr << "Can't open '" << filename << "' file.\n";
            exit (1);
        } 
    
        for (int i = 0, r = rand () % 5; i <= r; ++i)
        {
            answer.clear ();
            char c;
    
            while (file.get (c).good () && c != '\n')
            {
                if (c == 'i') c = 'I'; // Replace character? :)
                answer.append (1, c);
            }
        }
    
        return answer;
    }
    
    int
    main ()
    {
        srand (time (NULL));
    
        string question;
    
        cout << "Please enter a question: " << flush;
        cin >> question;
        cout << answer (question) << endl;
    }
    

    … the only thing is that I have no idea why do you need to read string char by char in order to modify it. You can modify std::string object, which is even easier. Let’s say you want to replace “I think” with “what if”? You might be better off reading more about
    std::string and using find, erase, replace etc.

    UPDATE 2:

    What happens with your latest code is simply this – you open a file, then you get its content character by character until you reach newline (\n). So in either case you will end up reading the first line and then your do-while loop will terminate. If you look into my example, I did while loop that reads line until \n inside a for loop. So that is basically what you should do – repeat your do-while loop for as many times as many lines you want/can get from that file. For example, something like this will read you two lines:

    for (int i = 1; i <= 2; ++i)
    {
        do
        {  
    
            answerfile.get (answer);
            cout << answer << " (from line " << i << ")\n";
        }
        while (answer != '\n');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote code below that is very similar to the accepted answer here .
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
I write the code below to delete a file: FileInfo file = new FileInfo(filename);
I wrote some code with a lot of recursion, that takes quite a bit
I wrote some code to read a file in my Java Servlet class. (I'm
Since virturl directory name is not fixed, I wrote code below to include .css
I wrote code that fails to use the JQuery autocomplete to fire a result
I wrote below code to create an extra button on Calculator, but the button
In writing a response, I wrote some code that challenged my assumptions on how
I use the below code to write code to query a web method in

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.