fstream

Where to place file in order to read?

情到浓时终转凉″ 提交于 2019-12-31 00:34:42
问题 Hey, where do I place a text file that I'm trying to read using fstream? In this tutorial, http://www.gamedev.net/reference/articles/article1127.asp, they say ifstream fin("input.txt"); where would "input.txt" be located? Before I tried directing a path to the file by doing this "C:\Users\XXXXXXX\Documents\test.in". This however does not seem to work, Incorrect data input with fstream. I'm using CodeBlocks. Thanks in advance. 回答1: input.txt should be in the working directory. Usually the

fstream::open() Unicode or Non-Ascii characters don't work (with std::ios::out) on Windows

此生再无相见时 提交于 2019-12-30 10:26:40
问题 In a C++ project, I want to open a file ( fstream::open() ) (which seems to be a major problem). The Windows build of my program fails miserably. File "ä" (UTF-8 0xC3 0xA4) std::string s = ...; //Convert s std::fstream f; f.open(s.c_str(), std::ios::binary | std::ios::in); //Works (f.is_open() == true) f.close(); f.open(s.c_str(), std::ios::binary | std::ios::in | std::ios::out); //Doesn't work The string s is UTF-8 encoded, but then converted from UTF-8 to Latin1 (0xE4). I'm using Qt, so

does fstream read/write move file pointer

拜拜、爱过 提交于 2019-12-30 09:36:10
问题 This is kind of a simple question that I hope can be answered easily, do the file stream read and write operations move the pointer along? As an example: cpos=10000; for (i=0;i<20;i++) { dataFile.seekg(cpos+i,ios::beg); dataFile.read(carray[i],1); } Is it identical (logically) to: dataFile.seekg(cpos,ios::beg); cpos=10000; for (i=0;i<20;i++) { dataFile.read(carray[i],1); } In other words, does carray[] contain the same contents regardless of which method is used (I can't see the first method

C++文件读写详解(ofstream,ifstream,fstream)

删除回忆录丶 提交于 2019-12-29 04:44:35
C++文件读写详解(ofstream,ifstream,fstream)    这里主要是讨论fstream的内容: 1 #include <fstream> 2 ofstream //文件写操作 内存写入存储设备 3 ifstream //文件读操作,存储设备读区到内存中 4 fstream //读写操作,对打开的文件可进行读写操作 1、打开文件    在fstream类中,成员函数open()实现打开文件的操作,从而将数据流和文件进行关联,通过ofstream,ifstream,fstream对象进行对文件的读写操作    函数:open() 1 public member function 2 3 void open ( const char * filename, 4 ios_base::openmode mode = ios_base::in | ios_base::out ); 5 6 void open(const wchar_t *_Filename, 7 ios_base::openmode mode= ios_base::in | ios_base::out, 8 int prot = ios_base::_Openprot); 9 /* 10 参数:filename 操作文件名 11 mode 打开文件的方式 12 prot 打开文件的属性 //基本很少用到

C++文件操作详解(ifstream、ofstream、fstream)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-29 04:44:17
C++ 文件操作详解( ifstream 、 ofstream 、 fstream ) C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ostream引申而来) ifstream: 读操作(输入)的文件类(由istream引申而来) fstream: 可同时读写操作的文件类 (由iostream引申而来) 打开文件 (Open a file) 对这些类的一个对象所做的第一个操作通常就是将它和一个真正的文件联系起来,也就是说打开一个文件。被打开的文件在程序中由一个流对象(stream object)来表示 (这些类的一个实例) ,而对这个流对象所做的任何输入输出操作实际就是对该文件所做的操作。 要通过一个流对象打开一个文件,我们使用它的成员函数open(): void open (const char * filename, openmode mode); 这里filename 是一个字符串,代表要打开的文件名,mode 是以下标志符的一个组合: ios::in 为输入(读)而打开文件 ios::out 为输出(写)而打开文件 ios::ate 初始位置:文件尾 ios::app 所有输出附加在文件末尾 ios::trunc 如果文件已存在则先删除该文件 ios::binary 二进制方式 这些标识符可以被组合使用,中间以”或”操作符(|

c++文件流基本用法(fstream, ifstream, ostream)

无人久伴 提交于 2019-12-29 04:44:00
原文链接 前言: c++的文件流处理其实很简单,前提是你能够理解它。文件流本质是利用了一个buffer中间层。有点类似标准输出和标准输入一样。 c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。 有错误和疏漏的地方,欢迎批评指证。 需要包含的头文件: <fstream> 名字空间: std 也可以试用<fstream.h> fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。 ifstream -- 从已有的文件读 ofstream -- 向文件写内容 fstream - 打开文件供读写 支持的文件类型 实际上,文件类型可以分为两种: 文本文件和二进制文件. 文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。 例一: 写文件 声明一个ostream变量 调用open方法,使其与一个文件关联 写文件 调用close方法. #include <fstream.h> void main { ofstream file ; file . open ( "file.txt" ) ; file<< "Hello file /n " << 75 ; file . close ( ) ; } 可以像试用cout一样试用操作符<

eof problem c++

走远了吗. 提交于 2019-12-28 07:02:09
问题 i am using Dev C++ on windows xp #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string STRING; ifstream infile; infile.open ("sample.txt"); while(!infile.eof) { getline(infile,STRING); cout<<STRING; } infile.close(); return 0; } this codes gives the following error C:\C++\read.cpp: In function `int main()': C:\C++\read.cpp:11: error: could not convert `infile.std::basic_ios<_CharT, _Traits>::eof [with _CharT = char, _Traits = std::char_traits<char>

C++: Getting random negative values when converting char to int

六月ゝ 毕业季﹏ 提交于 2019-12-25 19:58:14
问题 Why do I get negative values when I'm trying to convert chars from the string read from file to int? fin.getline(text, 512); fin.close(); someInt = (int)text[0]; // It happens to be the random value and always negative. WHOLE CODE: The problem itself is in the Decrypt function. When I read the string from file, and convert chars from that string to int, i get negative values, always random, even if characters are the same. // WUEncryptor.cpp : Defines the entry point for the console

Reading binary file with fstream

扶醉桌前 提交于 2019-12-25 18:31:43
问题 I am reading a binary file with fstream and storing the information in an array of characters: int dataLength = 32; int counter = 0; char data[dataLength]; char PMTone[dataLength/4]; std::fstream *fs = new std::fstream(inputFileName,std::ios::in|std::ios::binary); fs->read((char *)&data, dataLength); //of the 32 characters in data[], I need first, 5th etc elements: //fill first pmt info for(int i=0; i<(dataLength/4); i++){ PMTone[i]=data[counter]; counter+=4; } Now I'm setting PMTone[7] to be

C++ ofstream not writing to output file?

余生长醉 提交于 2019-12-25 17:18:12
问题 The code is supposed to count the number of a, b, c, d, e, and f characters in the input text file and print the output into a second text file. When I run the code, it creates the output file but doesn't write anything into it. #include<iostream> #include<fstream> #include<cmath> using namespace std; int main(){ // establish counters for the number of each character char x; int acount=0; int bcount=0; int ccount=0; int dcount=0; int ecount=0; int fcount=0; ifstream iFile("plato.txt"); /