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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:18:02+00:00 2026-05-14T07:18:02+00:00

I am trying to open a file which normally has content, for the purpose

  • 0

I am trying to open a file which normally has content, for the purpose of testing i will like to initialize the program without the files being available/existing so then the program should create empty ones, but am having issues implementing it. This is my code originally

void loadFiles() {
fstream city;
    city.open("city.txt", ios::in);
fstream latitude;
    latitude.open("lat.txt", ios::in);
fstream longitude;
    longitude.open("lon.txt", ios::in);
while(!city.eof()){
    city >> cityName;
    latitude >> lat;
    longitude >> lon;
    t.add(cityName, lat, lon);
}
city.close();
latitude.close();
longitude.close();
}

I have tried everything i can think of, ofstream, ifstream, adding ios::out all all its variations. Could anybody explain me what to do in order to fix the problem. Thanks!

  • 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-14T07:18:02+00:00Added an answer on May 14, 2026 at 7:18 am

    You have posted code for reading files, not creating empty ones – you need t expand your question in this regard. And what you have posted is not good code for reading. The eof() function detects end of file following a read, not before one – basically, you should almost never use it. Instead, you should test the success of each read:

    while( (city >> cityName) && (latitude >> lat) && (longitude >> lon) {
        t.add(cityName, lat, lon);
    }
    

    Also, if you want to create an input stream, why not do so explicitly using an ifstream object:

    ifstream city( "city.txt" );
    

    No need to mess around with ios flags. You should really test if the open worked too:

    if ( ! city.is_open() ) {
       throw "open failed" ;   // or whatever
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to open a Word file for reading using a FileStream in
I would like to know whether its possible to open Open word 2003 (doc)
I have a text file which does not confirm to standards. So I know
Here's what I am trying to accomplish - I have about a million files
I'm trying to add existing files as links to a Website project in Visual
I have a HUGE html which has many things I don't need, but inside
I'm trying to use use the LoadInk method of an mathinputcontrol but I can't
I'm trying to load pickled objects in iPython. The error I'm getting is: AttributeError:
It's me your junior programmer extraodinaire I'm trying to display an image from my
I'm having a little problem getting my data to submit. I'm trying to make

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.