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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:48:52+00:00 2026-05-23T08:48:52+00:00

Disclaimer: this question is directly related to my programming homework. My C++ assignment consists

  • 0

Disclaimer: this question is directly related to my programming homework.

My C++ assignment consists of opening a .txt file, performing a bunch of operations on it, and then saving the .txt file. Problem is, I’m having a hard time just grasping the basic concepts of reading and writing files.

My code:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ifstream inData;
    ofstream outData;

    // is it necessary to open datalist.txt for both the in and out streams?
    inData.open ("datalist.txt");
    outData.open("datalist.txt");

    if (inData.is_open()) { 
        cout << "yay, i opened it\n"; // this outputs as expected

        char fileData[100]; // have to use char arrays as per instructor. no strings
        inData >> fileData; // store text from datalist.txt in fileData char array
        cout << fileData; // nothing happens here... why?

        outData << "changing file text cause I can"; // this works just fine.
    }
    else {
        cout << "boo, i couldn't open it";
    }

    inData.close();
    outData.close();

    return 0;
}

The main issue I’m encountering is that I don’t understand how to read the data in a file at even a basic level, let alone parse the file into relevant information (the purpose of the program is to read, write, and manipulate information in a semi-colon delimited list).

In addition to this question, I also am a little confused on two other things. First, is it necessary to open datalist.txt for both the in and out streams, for some reason it just feels weird that I have to open the same file twice. Second, my instructor doesn’t want us to use the string class, and instead use char arrays. I don’t understand the logic behind this and was hoping someone could elaborate why (or perhaps give a counter argument to why) strings are bad.

  • 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-23T08:48:52+00:00Added an answer on May 23, 2026 at 8:48 am

    You don’t open a file for reading and writing at the same time. Well, not with two different objects that don’t know about each other at any rate. You either use a std::fstream (which can do simultaneous reading and writing), or you read first, close the file, process the data, then write it.

    Also:

    //have to use char arrays as per instructor. no strings

    I think you may want to get a better instructor. The use of a naked, stack-based char* array is not something that any C++ teacher worth their salt should endorse.

    This is where buffer overruns come from.

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

Sidebar

Related Questions

Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer This is not strictly a programming question, but most programmers soon or later
Full disclaimer: I'm a CS student, and this question is related to a recently
( Disclaimer: This question is not specific to ASP.NET) I have a control which
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less
Disclaimer This question is a repost. I originally asked it here . While there
Disclaimer : This is a very basic question, but keep in mind I come
Disclaimer: This is a follow-on question from my other question about NServiceBus which was
Disclaimer : this is not a how to question. I would more like to

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.