fstream

How to use fstream objects with relative path?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 18:37:11
问题 Do I always have to specify absolute path for objects instantiated from std::fstream class? In other words, is there a way to specify just relative path to them such as project path? 回答1: You can use relative paths as well. But they are relative to the environment you call your executable from. This is OS dependent but all the major systems behave more or less the same AFAIK. Windows example: // File structure: c:\folder\myprogram.exe c:\myfile.txt // Calling command from folder c:\folder >

fstream seekg(), seekp(), and write()

余生长醉 提交于 2019-12-17 18:34:34
问题 I'm looking for some clarification on how seekg() and seekp() works with respect to when you are writing to a file. Say for instance I had a file like so: offset 0: 2 offset 4: 4 offset 8: 6 offset 12: 8 offset 16: 10 Now I want to open the file and do some seeks to read and write values. fstream file; file.open("file.txt", fstream::in |fstream::out | fstream::binary); file.seekp(0, ios::end) // seek to the end of the file int eofOffset = file.tellp(); // store the offset of the end-of-file,

std::ofstream, check if file exists before writing

六月ゝ 毕业季﹏ 提交于 2019-12-17 15:34:16
问题 I am implementing file saving functionality within a Qt application using C++. I am looking for a way to check to see if the selected file already exists before writing to it, so that I can prompt a warning to the user. I am using an std::ofstream and I am not looking for a Boost solution. 回答1: This is one of my favorite tuck-away functions I keep on hand for multiple uses. #include <sys/stat.h> // Function: fileExists /** Check if a file exists @param[in] filename - the name of the file to

Partially truncating a stream (fstream or ofstream) in C++

早过忘川 提交于 2019-12-17 14:53:08
问题 I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... 回答1: I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE * , at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one). You may find this question to be of assistance.

Partially truncating a stream (fstream or ofstream) in C++

吃可爱长大的小学妹 提交于 2019-12-17 14:53:03
问题 I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated... 回答1: I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE * , at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one). You may find this question to be of assistance.

std::fstream doesn't create file

萝らか妹 提交于 2019-12-17 03:41:12
问题 I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist. std::fstream my_stream my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out); if(!my_stream) std::cout<<"error"<<strerror(errorno); I get this result: "No such file or directory." How can I create the file in this case? 回答1: You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.

mmap() vs. reading blocks

筅森魡賤 提交于 2019-12-16 20:06:18
问题 I'm working on a program that will be processing files that could potentially be 100GB or more in size. The files contain sets of variable length records. I've got a first implementation up and running and am now looking towards improving performance, particularly at doing I/O more efficiently since the input file gets scanned many times. Is there a rule of thumb for using mmap() versus reading in blocks via C++'s fstream library? What I'd like to do is read large blocks from disk into a

机器学习(一)

自古美人都是妖i 提交于 2019-12-16 07:34:24
机器学习第一天: 决策树: #include<opencv2/opencv.hpp> #include<opencv2/ml.hpp> #include<iostream> #include<fstream> #include<cstdio> using namespace std; using namespace cv; using namespace cv::ml; //读取文件中的点坐标 int readFile(vector<Point> &trainedPoints, vector<int> &trainedPointsMarkers) { ifstream in; in.open("points.txt"); if (!in.is_open()) { cout << "Could not open the file" << "points.txt" << endl; cout << "Program terminating.\n"; return 0; } int flge = 0; int fpoint, flabel; Point point; while (!in.eof()) { in >> fpoint; //依次为横坐标,纵坐标,分类 if (( flge % 3 == 0 ? point.x = fpoint : flge % 3 == 1 ? point

fstream EOF unexpectedly throwing exception

。_饼干妹妹 提交于 2019-12-13 16:22:09
问题 My question is very similar to a previous one. I want to open and read a file. I want exceptions thrown if the file can't be opened, but I don't want exceptions thrown on EOF. fstreams seem to give you independent control over whether exceptions are thrown on EOF, on failures, and on other bad things, but it appears that EOF tends to also get mapped to the bad and/or fail exceptions. Here's a stripped-down example of what I was trying to do. The function f() is supposed to return true if a

fstream doesn't resolve path

早过忘川 提交于 2019-12-13 07:43:05
问题 i am new to c++ and trying to write a simple function, that saves a string to a file. The function works, when i pass the full path to fstream, but it doesn't resolve relative paths. Here is the relevant part of my code #include <iostream> #include <fstream> void writeToFile () { std::fstream fs; fs.open ("/home/blabla/Documents/test.txt", std::fstream::in | std::fstream::out | std::fstream::app); fs << " test content"; fs.close(); } This works fine, but i would like to create the file in the