fstream

Not getting all lines from a text file when using getline() in C++

冷暖自知 提交于 2019-12-11 20:35:28
问题 I was given a homework assignment to generate a txt file containing a random number of lines, each with a random amount of integers, ranging between a minimum value and a maximum value. Lots of rand() fun. In any case, that was the easy part. The second part of the problem is to read over the first file and create a second file that contains some statistics, such as: the sum of all integers in the file, their average, min and max values, and my main issue: the sum of all integers in each line

Read Specific Entry From Text File

我是研究僧i 提交于 2019-12-11 19:42:31
问题 This is the text file named "TextFile.txt" Stat Rain 1 16 2 34 3 24 4 23 5 21 6 19 7 17 8 35 9 27 And here is my c++ code: #include<iostream> #include<fstream> using namespace std; char Station[9]; char Rainfall[9]; int i; int j; int s[23]; double r[23]; main() { ifstream Read; Read.open("TextFile.txt"); Read>>Station;//I don't want this Read>>Rainfall;//I have no choice then I just assign these two variables i=0; while(!Read.eof())//This is the only I want { i++; Read>>s[i]>>r[i]; } Read

How to compare new string name with existing from txt file?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 16:59:41
问题 I want to implement a simple function in a voting program that is searching for a name, and if this name is already existing then it will show a message that a person cannot vote. But I'm so confused with txt files. The code below does not work properly, I want to understand what I need to do. Also, how to find a full name? I think it is only searching for the first word bool searchname(string mainvoter); int main() { ofstream newvoter("voter.txt", ios::app); string name; cout<<"Enter your

Passing/giving commands to CMD through Qt GUI

自闭症网瘾萝莉.ら 提交于 2019-12-11 16:47:46
问题 What I'm trying to achieve (And struggling with) is basically passing commands to CMD through my QT Mainwindow application. I would like to have my code, first run CMD (preferably hidden). I used here QProcess like this: (in my Mainwindow.cpp file) QString exePath = "C:/Windows/System32/cmd.exe"; QProcess pro; pro.startDetached(exePath); pro.waitForStarted(); this question helped me a lot However, what this answer/question lacks is actual help on "appending" commands to CMD (not sure if this

What am I doing wrong with my serializing a vector with structs in it to a .dat file?

只愿长相守 提交于 2019-12-11 15:12:03
问题 If I type in Description: Apple Quantity: 10 Wholesale Cost: 30 Retail Cost: 20 Date Added: December These are the contents in my .dat file: 1Apple103020December But when I load my program, it doesn't load the struct back in correctly resulting in there being 0 items in my list. Is that what it is suppose to look like or am I doing something seriously wrong. Code: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <vector> using namespace System; using

On comparing different way of reading an integer from a file

邮差的信 提交于 2019-12-11 14:58:25
问题 While reading this question, I found in the answers different way of reading an integer. Here are two of them. The one from πάντα ῥεῖ's answer: if(iFile) { iFile >> int1; cout << "From first file:" << int1 << endl; } The one from JRowan's answer: if(!iFile.eof()){ iFile >> int1; cout << "From first file:" << int1 << endl; } But I was wondering if using the code below is equivalent to the two above? Also, is there any reason not to use one of the three sample codes? if (iFile >> int1) { cout <

Issues saving double as binary in c++

假装没事ソ 提交于 2019-12-11 12:15:21
问题 In my simulation code for a particle system, I have a class defined for particles, and each particle has a property of pos containing its position, which is a double pos[3]; as there are 3 coordinate components per particle. So with particle object defined by particles = new Particle[npart]; (as we have npart many particles), then e.g. the y-component of the 2nd particle would be accessed with double dummycomp = particles[1].pos[1]; To save the particles to file before using binary I would

How to open a file using ifstream and keep reading it until the end

泄露秘密 提交于 2019-12-11 10:39:47
问题 I simply would like to open a file named "test.txt" that contains: cat dog moose rabbit I then want to read from the file and convert its contents into strings for things that I will do later in the program. So far i think what I am doing is going in the right direction. However, I am getting an error and I am unsure what to do. Here is the Error>>> $ make -f makefile.txt g++ -g -D HASH_TABLE_SIZE=10 -c hash.cpp hash.cpp: In member function `void Hash::processFile(std::string)': hash.cpp:12:

outFile C++ not writing to output text

微笑、不失礼 提交于 2019-12-11 10:38:06
问题 Down below is my incomplete program. I am having problems with writing to a text file. For example I want to write the number of snow days to a text file, but nothing shows up in the textfile when I debug in VS 2010. It does display my info and name, but nothing else works. It wont write anything after that. its NOT writing to a text file. #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; const string INFORMATION = "College Class"; const string

Endianness for floating point

折月煮酒 提交于 2019-12-11 10:21:50
问题 I'm writing and reading binary data ( std::ios::binary ) in C++ using std::fstream - this includes integer and floating point values. While my code works on my own architecture, I wan't to make sure, that it's portable and i.e. binary files written on my machine shall still be read properly of an machine with different endianness. So my idea was that I will add in the binary file at first byte a value which will indicate the endianness of the file. As there is no guarantee, that endianness of