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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:07:40+00:00 2026-06-17T12:07:40+00:00

sorry im real new to c++ and can’t seem to understand the basic concept

  • 0

sorry im real new to c++ and can’t seem to understand the basic concept of reading a txt file and writing one. i have a program that currently just prompts the users and then delivers the output on the command line after reading the data. but i want to have it read a file, and the create a file with its output

heres it so far

#include <iostream>
#include <string>

using namespace std;

int main()
{

    cout << "Enter number of names \n";
    int a;
    cin >> a;
    string * namesArray = new string[a];
    for( int i=0; i<a; i++) {
        string temp;
        cin >> temp;
        namesArray[i] = temp;
    }

    for( int j=0; j<a; j++) {
        cout << "hello " << namesArray[j] << "\n";

    }
    return 0;
}

Thanks to all..

  • 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-17T12:07:41+00:00Added an answer on June 17, 2026 at 12:07 pm
    #include <fstream>
    #include <string>
    #include <iostream>
    using namespace std;
    int main(){
        string fileName = "test.txt";        
        //declare a file object
        //options are:
        //ofstream for output
        //ifstream for input
        //fstream for both
        fstream myFile;
        //cannot pass a c++ string as filename so we convert it to a "c string"
        //... by calling strings c_str member function (aka method)
        //the fstream::out part opens the file for writing
        myFile.open(fileName.c_str(), fstream::out);
        //add the text to the file end the line (optional)
        myFile << "Some text" << endl;
        myFile.close();//always close your files (bad things happen otherwise)
        //open the file for reading with fstream::in
        myFile.open(fileName.c_str(), fstream::in);
        string myString;
        //get a line from a file (must be open for reading)
        getline(myFile,myString);
        myFile.close();//always close your file
        //demonstrates that it works
        cout << myString;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry I am very new to open graph and have been having difficulty to
New to Haskell so sorry if this is very basic This example is taken
I am in a real trouble here to read a large txt file (around
I am sorry if this is more of a theory question then a real
[Updated, sorry about the change but now to the real problem] I cannot include
Sorry for question but I can't find answer anywhere on internet. I couldn't find
Sorry for this simple question, but I can't solve it... There is an example:
Sorry, this is probably a duplicate question, but how can I iterate over a
Here is a real version control system dummy! proper new starter! The way I
Edit: This was user error (sorry). I have answered it below. I am developing

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.