C ++ reading data from a file

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.

+4
source share
2 answers

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

also:

// you need to use char arrays according to the instructor. no lines

I think you might want to get a better instructor. Using a stack-based bare char* array is not something that any C ++ teacher who deserves their salt should approve of.

This is where the buffer overflows.

+3
source

Opening the same file for reading and writing through two different file objects is usually a bad idea. In your case, this also led to (part of) your problem. By default, opening ofstream truncates it. So when you read inData , you get nothing. This is why nothing happens here:

 cout << fileData; // nothing happens here... why? 

At the end, your file contains:

 changing file text cause I can 

And nothing else.

So, in order to read a file, you should not open it for writing as it is. If you want to change the text of the file only to your own line, you can simply perform two separate operations. Open inData , read it and close it. Now open outData , write our line and close it.

On the other hand, if you want to add a line to the end of an existing file, you must open one stream for reading and writing. Read to the end of the file, then write a line with the file pointer at the end.

This is the main idea. And yet, and I will do the homework for you. :)

+1
source

All Articles