Questions tagged [fstream]
fstream provides an iostream interface for file I/O in C++.
2,571
questions
-1
votes
2answers
29 views
Check all lines in the file that have both, one or none of the strings in the same line [closed]
I want to check if the chosen strings appear in the same line.
Sadly I don't get the correct output.
The file contains this text
/* one
two
one two
two one
two
one
something
else */
#include <...
0
votes
1answer
42 views
Writing/Reading the contents in a Memory Stream instead of File Stream in C++
I want to write and read the contents to a memory stream instead of any file stream like fstream, ifstream or ofstream. So that all the contents which should be written in a file should be there in ...
0
votes
0answers
37 views
overwrite file only at the first actual write
I would like to open a file for writing, such that the file is wiped and written over at the first write instruction. That is, the file should retain its contents when opened, and only at the first ...
-2
votes
0answers
19 views
Parse through CSV and remove items within “ ” [closed]
I am currently trying to parse through a csv and trying to remove all data in a csv that is within quotation marks like such:
"%d %s @ %.2f", 17000, CAT, @ 4.72
would return
17000 CAT @ 4.72
...
0
votes
3answers
33 views
Sorting out int variables from a .txt file (fstream, c++)
I have a .txt file with these this line on the top '12 4 25 257' each of the numbers spaced out by a ' ' and the line ends with '\n'
I have these variables and a function getFirstLine:
int A;
int B;
...
-1
votes
0answers
36 views
reading a text file into string c++ it doesn't print the first line
i am a beginner in c++ just testing new things ...
any way i have problem with reading a text file in c++ into a string ..
when i do that it doesn't print the first line ..
this is my code
else if (i ...
4
votes
1answer
65 views
Does std::stringstream has two buffers and hence two markers?
On C++ primer chapter 17; I/O streams; it is said that an object of type std::iostream or a derived type like std::fstream, std::stringstream support both reading and writing. Also these types have ...
0
votes
1answer
22 views
Can I read and write to the same file in Android, using mutexes, without having undefined behaviour?
I'm designing a musical Looper, which is something that, once a first recording is made with x seconds, repeats playing these x seconds and on each iteration, adds new content to the loop.
Since the ...
-2
votes
0answers
27 views
Newline characters in .txt saved as \u000D\u000A, ruining my code, which refers to them as '\n'. How to fix? [duplicate]
I'm creating a program that reads in a text file. Each separate line in the file represents a new string within the program; each string would be saved to the same vector.
Specifically, I would save ...
0
votes
0answers
28 views
converter extension in c++
I was trying to create a program using the lib fstream to take all lines of one documents like main.cpp or main.docx and convert that to a file with .txt, like copy and paste. However, I'm having ...
0
votes
1answer
28 views
How to measure the remaining bytes on std::fstream?
This is how I open my std::fstream:
f.open(filePath, std::ios_base::binary | std::ios_base::in |
std::ios_base::out);
After calling some reads, how can I know how ...
0
votes
0answers
36 views
First character of cstring removed during ifstream c++
The first character of cstring keeps being removed upon the second execution of getline in ifstream:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char ...
0
votes
0answers
32 views
How to write to file outside working directory?
I'm trying to figure out how to write to a file outside the working directory. This is the code I currently have.
#include <iostream>
#include <fstream>
#include <string>
int main()
...
0
votes
0answers
29 views
regex_match not returning any expected matches
So i'm trying to make a program that searches for the number of <nodes in a file using regex_match. I've jumped through all the hoops but nothing seems to turn up a match.
string str;
int numNodes =...
-1
votes
2answers
70 views
How do you make it so CPP checks if a user has a file already on their system?
How to see if a user already has a file? For example, I want this to make a text file and if a user already has a file in their system for it not to work and send an error.
#include <iostream>
#...
0
votes
0answers
37 views
Ifstream not reading entire text file in C++
I'm trying to read a text file with ifstream and write it to another text file. But either the input text file is read partially, or is written partially to the output file. Only a handful of ...
-1
votes
0answers
80 views
C++ File reading doesn't return a value
So I have a file test.cpp, inside I have this code:
#include <iostream>
#include <fstream>
using namespace std;
string readFromFile(string pathToFile, int lineNumberStart, int ...
-2
votes
0answers
25 views
How to delete line from text file and also replace content from that line [duplicate]
I have a text file with a list each line having a student ID, major, and gpa. Essentially, I want to be able to delete a "student" from the file, based on the ID the user wants to delete ...
0
votes
2answers
53 views
fstream::open() doesnt work - error reading characters from string
I am using vs2012 and need to take user input as file name to open the file.
(I use using namespace std; in the beginning of my programs so cout << basically means std::cout)
This code works for ...
0
votes
2answers
52 views
Order of includes with standard libraries and local headers
I'm generating bitmap file. The program compiles every time but depending on order of #include at some point, it gives me good or corrupted .bmp file.
It is from a tutorial and all the file are here.
...
0
votes
1answer
32 views
Need to use fstream to take input from “.in” files and store different data types
Basically what I'm aiming for is to take input from a "new.in" file that look something like:
5 1 2 1
3 onion pepper olive
3 mushroom tomato basil
3 chicken mushroom pepper
3 tomato mushroom ...
-1
votes
1answer
37 views
How to delete a specific line/string from a text file (fstream/c++)
This function is meant to remove a specific line from a database text file.
void removeEntry() {
fstream outputFile("outputFileName", std::fstream::in | std::fstream::out | std::fstream::...
0
votes
0answers
20 views
How to read txt files into an array that are stored into class variables? [duplicate]
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class BasketballPlayer
{
...
-1
votes
1answer
41 views
fstream not reading character by character but whole numbers
void Map::LoadMap(std::string path, int sizeX, int sizeY) {
char c;
std::fstream mapFile;
mapFile.open(path);
int srcX, srcY;
for(int y = 0; y < sizeY; y++) {
...
0
votes
0answers
23 views
About problem multi user and permission denied
I have a application, that run on multi-user (Sample: if have 3 user signed in, we will have 3 process). But, today i received a error when open (or create) 1 file by std::fstream: permission denied.
...
0
votes
1answer
23 views
Paste contents of a file to another file from a particular line
I want to copy content of a text file (description.txt) and paste it on another text file at a particular line (movie.txt) using c++.
Here's the file contents:
description.txt
Chris Gardner takes up ...
1
vote
1answer
28 views
Reading file with fstream then closing and reading again wont work on Android
I'm having a problem that if I write to a file and read from it, then everything is fine. But then if I close it and reopen it, all the data is gone.
It's running on Android and the filePath2 is from ...
0
votes
1answer
75 views
how exactly input and output file streams work in c++
I came across the following observation while working with input and output file streams and I am confused. Can anyone tell me why this happens:
I saved a txt file on my desktop by the name hello ...
-2
votes
2answers
53 views
OpenGL error(#97) No program main found even though the shader is correct and successfully read from a file
I was building my own shader based on the https://learnopengl.com/ tutorial.
The problem was that I have the following error when linking the shader program
VERTEX SHADER LOG
FRAGMENT SHADER LOG
...
0
votes
0answers
36 views
How to overwrite a file with fstream
I have 2 fstream objects called oldFile and newFile and I want to find the non-Null elements in oldFile and write them into newFile of a destructor. The name of the file would be exactly the same &...
0
votes
0answers
15 views
Data corrupted after re-reading file in fstream
Sorry if I didn't phrased the question correctly. Basically I'm declaring an array of data structure after reading total items in a .txt file in a management system. Every line has an id/sr# in order. ...
0
votes
0answers
8 views
fstreamlibrary code- works fine on visual studio code but doesnt work on other coder checkers
this code works on visual studio code but the automatic code checker gives me 30/80 can anyone identify whats wrong?
// program reads from a file and determines how often each of the decimal digits ...
-2
votes
1answer
68 views
How to set the precision while writing binary file in c++?
I need to get the data in .pcd format into .bin format(data is lidar point clouds). So I am trying to write the data in a row aligned order with precision. But I could able to set the precision at all....
0
votes
1answer
65 views
How to remove a file from folder [closed]
Im working for a school project in which fstream is included, but the problem is that i cannot delete a specific file in another folder, here's the code:
cout << "(Enter Y to ...
0
votes
2answers
32 views
How to prepare a code for execution on a cluster so that it takes one parameter from a .txt file at a time?
I am preparing some C++ code to be run on a cluster, managed by SLURM. The cluster takes one compiled file: a.out. It will then execute it on 500 different nodes via the JOB_ARRAY. When executing each ...
0
votes
1answer
31 views
C++ Im trying to stream a file, and replace the first letter of every line streamed. It doesn't seem to be working as expected
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
void add1(std::fstream& files)
{
char c;
int i=0;
int j=0;...
0
votes
1answer
59 views
read the file that is opened with my console application in C++ [closed]
I have written a console application in C++ and I need to read the file that is opened with my console app:
right click on txt file
select open with
choose my console application
then I want to read ...
0
votes
0answers
19 views
How to read the input with space in between that's coming from an ifstream file?
Link to the .txt file that's supposed to be the input
As the caption says, How do I read the input with space in between that's coming from an ifstream file?
In this situation I cant use getline ...
0
votes
0answers
66 views
Read ASCII file in C++
I've got a text file and have to parse it in C++. I do it in this way:
std::string line;
std::fstream inputFile;
inputFile.open("test");
while(std::getline(inputFile, line))
{
std::cout &...
21
votes
2answers
586 views
Why does GCC's ifstream >> double allocate so much memory?
I need to read a series of numbers from a space-separated human-readable file and do some math, but I've run into some truly bizarre memory behavior just reading the file.
If I read the numbers and ...
0
votes
0answers
34 views
How to increase the buffer size used by rdbuf in c++
I am using below c++ code to copy a file (size is around 1.5-2 gb) from one location to another.
#include <fstream>
int main()
{
std::ifstream src("source_path/file.mp4", std::...
-1
votes
1answer
30 views
Problem with fstream and getting data from file
I have big problem with getting data from a file. I need to retrieve characters up to 1 space I encountered and write them to other file. I wrote something like this:
plik=fopen("firstfile.txt&...
0
votes
1answer
23 views
Understanding the error default definition would be ill-formed [duplicate]
#include <iostream>
#include <fstream>
void ReadFile(ifstream in);
int main() {
ifstream in("handout.txt");
ReadFile(in);
return 0;
}
void ReadFile(ifstream in){
...
-2
votes
1answer
29 views
Appending nodes to linked list by reading from text file almost working
Good day everyone! I have a program where it has a linked list where each node has a string name and an int for the ID's for the respective names, the append, prepend, insertafter, delete and etc. ...
0
votes
1answer
45 views
Extracting and storing data to ostream
Is it possible to store data to an ostream like this
void write(std::ostream& os){
int x,y = 0; bool b = true;
os<<x<<" "<<y<<" "<<b<<...
0
votes
0answers
30 views
trouble parsing text file with C++ [duplicate]
I have a .txt file containing a list of about 200 numbers with one on each line and I'm trying to parse it with C++ but I'm facing some issues -- here's my code:
#include <iostream>
#include <...
0
votes
1answer
45 views
How can I save different types of objects into a single file and read them as needed?
class Room{
public:
int price;
char type[20];
int roomNum;
int floor_num;
bool status; //reserved = true, not reserved = false
Room() : price(0) , ...
0
votes
0answers
42 views
Data structure to store integers, floats and text at the same time
I am simulating a DELETE command from SQL in c++. I've tried all the possible data types to temporary store the data from a certain table( a .bin file in my case), but none of them worked.
If I store ...
0
votes
0answers
17 views
Can't get fstream to read allocate an array size
I'm trying to use a void function to read from a text file and allocate the size of a vector struct.
This is my void function.
void allocateFlight( ifstream &inFile, string &item,vector<...
0
votes
0answers
35 views
ofstreamFile << ifstreamFile.rdbuf() fails but file text from file is written to ofstreamFile correctly
std::ifstream inFile;
std::ofstream outFile;
// ...
outFile << inFile.rdbuf();
I was writing one file into another with rdbuf, everything was good. I decided to add error check
std::ifstream ...