Disclaimer: This question is directly related to my homework programming.
The purpose of My C ++ is to open a .txt file, perform a bunch of operations on it, and then save the .txt file. The problem is that itโs hard for me to master the basic concepts of reading and writing files.
My code is:
#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 problem that I am facing is that I do not understand how to read data in a file at a basic level, not to mention parsing the file into relevant information (the goal of the program is to read, write, and manipulate the information in the list with delimiters with a comma).
In addition to this question, I am also a bit confused about two other things. Firstly, you need to open datalist.txt for incoming and outgoing streams, for some reason itโs just strange that I have to open the same file twice. Secondly, my instructor does not want us to use a string class, but instead use char arrays. I do not understand the logic behind this, and was hoping that someone could explain why (or perhaps give an argument to the argument why) the lines are bad.
source share