fstream

C++ Trouble Reading a Text File

耗尽温柔 提交于 2020-01-13 18:17:10
问题 I'm trying to read a text file but nothing is coming out. I feel like maybe It's not linking correctly in my Visual Studio Resources folder but if I double click it - it opens fine in visual studio and it doesn't run into any problems if I test to see if it opens or if it is good. The program compiles fine right now but there's not output. Nothing prints to my command prompt. Any suggestions? Code #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { char

Is opening the SAME file in two different fstreams Undefined Behaviour?

一个人想着一个人 提交于 2020-01-13 02:40:08
问题 This recently asked question has raised another interesting issue, as discussed in the comments to one of its answers. To summarize: the OP there was having issues with code like that below, when subsequently attempting to read and write data from/to the two streams 'concurrently': ifstream infile; infile.open("accounts.txt"); ofstream outfile; outfile.open("accounts.txt"); Although the issue, in itself, was successfully resolved, it has raised a question to which I cannot find an

How to use fgets if you don't know the number of characters to be read?

筅森魡賤 提交于 2020-01-12 05:11:09
问题 I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets() , or is there a better alternative? Note: char *fgets(char *str, size_t num, FILE *stream); 回答1: Don't forget that fgets() reads a line at a time, subject to having enough space. Humans seldom write lines longer than ... 80, 256, pick a number ... characters. POSIX suggests a line length of 4096. So, I

How to print the values of two dimensional pair vector?

泪湿孤枕 提交于 2020-01-11 13:33:33
问题 I have a text file with 4 rows and 3 columns (0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161) (0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445) (0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303) (0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175) I wrote following code to print the values of FFTfile . The script does not throw any errorm but it does not print the values. Any idea whats wrong? #include <iostream> #include <stdio.h> #include <fstream> #include <stdlib.h

How to print the values of two dimensional pair vector?

拜拜、爱过 提交于 2020-01-11 13:32:09
问题 I have a text file with 4 rows and 3 columns (0.165334,0) (0.166524,-0.0136064) (-0.144899,0.0207161) (0.205171,0) (0.205084,-0.0139042) (-0.205263,0.0262445) (0.216684,0) (0.215388,-0.0131107) (-0.193696,0.0251303) (0.220137,0) (0.218849,-0.0135667) (-0.194153,0.025175) I wrote following code to print the values of FFTfile . The script does not throw any errorm but it does not print the values. Any idea whats wrong? #include <iostream> #include <stdio.h> #include <fstream> #include <stdlib.h

C++ precision of numbers and truncation with fstream

霸气de小男生 提交于 2020-01-11 09:51:28
问题 I have a file.txt with hundreds of numbers. They have many digits (max 20) after the point and I need to get them all without truncation, otherwise they introduce errors in the following computations. I made these numbers with matlab so it has a monstrous precision but now I must replicate this behaviour in my program. I've done this way: fstream in; in.open(file.txt, ios::in); long double number; in>>number; I also tried this in.precision(20); in>>number; before each ">>" operation but it is

Problems writing an integer to binary file C++

China☆狼群 提交于 2020-01-07 03:07:55
问题 I am using fstream to write an integer to binary file. int main(){ fstream f1; int num = 2, num2 = 0; f1.open("dat1", ios::app | ios::out | ios::in | ios::binary); f1.write((char *)num, sizeof(int)); f1.seekp(0); f1.read((char *)num2, sizeof(int)); cout << num2; } The problem is on line f1.write. I can write to binary file an array, but when I try to write just one block of int it gives me an error: Unhandled exception at 0x522C7EA6 (msvcp120d.dll) in Project.exe: 0xC0000005: Access violation

Storing vector of pointers in a file and reading them again

眉间皱痕 提交于 2020-01-06 10:20:13
问题 Assume the following code: // Base class class Base { public: Base(int val) : hi(val) { } virtual void PublicMethod() { /* Do something */} private: int hi; }; // Child class class Child : public Base { public: Child(int val) : Base(val) { } void PublicMethod() { /* Do something */} }; // Vector of pointers to prevent slicing std::vector<std::shared_ptr<Base>> list; (...) // Fill list with mixed data of class Base and Child After the list is filled with objects, I want to store the data into

fstream not opening files with accent marks in pathname

北城以北 提交于 2020-01-06 07:24:51
问题 #include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, char **argv) { try { ifstream file; string simplePath = "I:/foo.conf"; string markPath = "I:/Folder_à/foo.conf"; file.exceptions(ifstream::failbit | ifstream::badbit | ifstream::eofbit); file.open(simplePath.c_str()); // ok file.open(markPath.c_str()); // exception ios_base::failbit set } catch (ifstream::failure f) { cout << "Exception " << f.what() << endl; return 1; } return 0; } If a file has

Trying to figure out why when writing to file, the last character written is being duplicated when cout looks fine on display

岁酱吖の 提交于 2020-01-05 09:34:20
问题 Question = Trying to figure out why the write to file for myfile.open ("Crypt103Data.txt", ios::out | ios::app); myfile <<tab2[code1]; myfile.close(); is writing the last character twice to Crypt103Data.txt when the cout which also runs for the same iterations within the while loop displays proper without the extra character write appended to file? If you enter something like Hello_World you might see something like r$cc7fq7UcX depending on what seed value to entered which gives the output a