由买买提看人间百态

topics

全部话题 - 话题: ifstream
首页 上页 1 2 (共2页)
f********u
发帖数: 572
1
来自主题: Programming版 - 这样读多个文件对吗?
ifstream in;
string filename[2] = {"a.txt", "b.txt"};
while (in) {
int j;
in.open (filename[j], ios::in);
j++;
}
编译错误。请问我错在哪里了?
f********u
发帖数: 572
2
来自主题: Programming版 - Re: [转载] 这样读多个文件对吗?
【 以下文字转载自 JobHunting 讨论区 】
【 原文由 flywithyou 所发表 】
哦,这个我改正了。
还有一个问题想请教。
我定义一个数组用来存放从文件中读取的数字。
ifstream in;
in.open ("a.txt", ios::in);
long int x[1000];
while (in) {
int i=0;
in>> x[i];
}
a.txt中一共有多少行数据我并不知道,我如何定义x[]的size呢?
而且可能达该
可能达该有几万个x[i]值,我怎么板呢?
谢谢
s******r
发帖数: 21
3
来自主题: Programming版 - 读取数据求教

Here is my solution. It is not necessarily the simplest one.
I use f.seek(-60, ios::end) here, assuming:
1. the data file is in Text Format;
2. the last 2nd number is always within the last 60 bytes of the data file.
The last 2nd element in the vector v is what you want, then.
#include
#include
#include
#include
using namespace std;
int main(void){
ifstream f("1.data");
f.seekg(-60, ios::end);
vector v((istream_iterator(f)), istream_itera
x*****u
发帖数: 3419
4
来自主题: Programming版 - 读取数据求教
感谢! 但感觉这个-60不好把握,多了少了都成问题。
用getline应该更稳妥一点。
1 // istringstream::str
2 #include
3 #include
4 #include
5 #include
6 using namespace std;
7
8 int main () {
9
10 double val;
11
12 string file_name("Tnve.dat");
13 ifstream infile(file_name.c_str(),ios::in);
14 istringstream iss;
15 string strvalues,sv;
16
b******y
发帖数: 2729
5
【 以下文字转载自 JobHunting 讨论区 】
发信人: buddyboy (hello), 信区: JobHunting
标 题: 【请教】fscanf 和 fstream 哪一个更好?
发信站: BBS 未名空间站 (Thu Feb 1 13:39:30 2007)
关于C++里面读写文件有很多做法,
最普遍的两种是:
#include
int mydata;
FILE *infile = fopen("data.dat","r");
fscanf(infile,"%d",&mydata);
或者使用:
#include
using namespace std;
int mydata;
ifstream infile("data.dat");
infile>>mydata;
哪一种方法更好更实用?谢谢!
g*******s
发帖数: 59
6
来自主题: Programming版 - 关于文件读取的C++ 问题?
i am using visual studio 2005 to test the use of getline.
The text file is like:
test.txt
a is a file
1.0 2.0
b isnot a file
3.0 4.0
The codes r
#include
#include
#include
using namespace::std;
void main()
{
string zz,zz1;
ifstream fin("test.txt");
getline(fin,zz);
double a[2],b[2];
fin>>a[0]>>a[1];
cout< // Now problem begins!
getline(fin,zz1);
fin>>b[0]>>b[1];
cout< }
Could you explain this t
g***e
发帖数: 577
7
来自主题: Programming版 - 问一个打开文件的问题
cin>>str;
ifstream f;
f.open(str);
编译时不能通过,因为open(const char* filename,int mode=1).怎么在执行时打开一
个文件.
文件名是执行的时候才能决定的
p**e
发帖数: 533
8
来自主题: Programming版 - 有人能解释一下这段C++代码吗
#include
main()
{
ifstream in("FileStreamTest.cpp");
while (in.get(*cout.rdbuf()))
in.ignore();
}
这个主要是什么意思?特别是in.get(*cout.rdbuf())这一个怎么解释?谢谢。
M**********n
发帖数: 432
9
【 以下文字转载自 Computation 讨论区 】
发信人: ManshengChen (像琼瑶一样老去), 信区: Computation
标 题: How to read binary(data) file generated by Fortran in C/C++?
发信站: BBS 未名空间站 (Sat Oct 13 19:28:21 2007)
How to read binary(data) file generated by Fortran in C/C++?
Is it possible? If possible how to realize it?
The binary file generated by Fortran is a double precision data file.
I have tried to use read binary file in C++, but have problem. The following
is my C++
code.
double temp=0.0;
ifstream File("bin.data",i
S*****n
发帖数: 227
10
来自主题: Programming版 - C++文件读取数值问题
这个简单。
ifstream ifs("a.txt");
ifs >> m;
for (int i=0; i<12; i++)
ifs >> a[i] >> b[i];
完了。
G****A
发帖数: 4160
11
但是我需要的是每次从文件中只读取一个整数. .txt文件的matrix形式纯粹是为了方便
看.就相当于c++:
ifstream input_file("E:\traffic.txt",ios::in);
for (line){
if (....){
....;
input_file>>graph[line[0]][line[5]];
....
....
c**********e
发帖数: 2007
12
来自主题: Programming版 - C++: How to read until the end of file?
I have a txt file, which are 0 and 1 separated by space.
input.txt:
1 0 1 0 0 0 0 0
0 1 1 1 1 0 1
I would like to count the number of 0's and 1's. I use
the following code. My question is how to modify the while(?)
to make it read the txt file until the end? Thanks a lot.
ifstream fin;
fin.open("input.txt");
int i=0, count0=0, count1=0;

while(i==0 || i==1)
{
fin >> i;
if(i==0) count0++;
if(i==1) count1++;
}
g*****u
发帖数: 298
13
来自主题: Programming版 - 这个面试题有什么trick?
如果中间某些扇区坏了这个应该怎么处理?是不是应该在开始的时候得到文件大小,如
果还没读到结尾就读不了了,把当前读了多少和文件大小比较,如果发现还没到末尾就
返回错误代码? 比如下面
ifstream fin( "temp.txt" );
unsigned long long num_bytes = 0;
while( fin.good() )
{
fin.get(ch);
num_bytes++;
.....
}
if ( num_bytes < file_length)
return -1;
如果中间某些扇区坏了那么good()应该返回0吧?我还没有实验过这种情况,不知道对
不对。
:读到一半硬盘坏了, heyhey...
r*******y
发帖数: 290
14
来自主题: Programming版 - How to read a simple csv file in C++?
std::ifstream ifs;
ifs.open("file_name");
char line[256];
std::string word1, word2;
while(ifs.getline(line, 256, ',') {
word1 = line;
ifs.getline(line, 256);
word2 = line;
std::cout << "word1 is" << word1 << "\t"
<< "word2 is" << word2 < }
c**********e
发帖数: 2007
15
来自主题: Programming版 - What is wrong with the code?
This is a very simple code. When I use two identical files (copy to get
an identical copy), it always says "NOT identical".
What is wrong? Thanks a lot.
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
ifstream fin1, fin2;
fin1.open(argv[0]);
fin2.open(argv[1]);
char c1, c2;
do {
fin1.get(c1);
fin2.get(c2);
if(c1!=c2) {
cout << "The two files are NOT identical." << endl;
b***y
发帖数: 2799
16
来自主题: Programming版 - [合集] 为什么10进制输出是负的?
☆─────────────────────────────────────☆
grasssu (没有昵称) 于 (Thu Nov 13 01:27:05 2008) 提到:
VC 8编译下列程序
//求文件大小
ifstream is( "Cars.mkv" );
is.seekg (0, ios::end);
streampos length = is.tellg();
//assert ( length > 0 ); //failed
cout.flags(ios::hex);
cout << length << endl; //输出正的 89c01076, 这个数字换成十进制是
2311065718是正确的。
cout.flags(ios::dec);
cout << length << endl; //输出-1983901578
为什么10进制就是负数呢?我怎么才能得到正确结果?
在ubuntu上面得到结果就是正确的。VC8为什么这么奇怪?
☆─────────────────────────────────────☆
redroof (yellow
b***y
发帖数: 2799
17
来自主题: Programming版 - [合集] read in file in c++ ,weird ?
☆─────────────────────────────────────☆
ruisher (aa) 于 (Sat Sep 10 00:05:17 2005) 提到:
I want to read in a file in c++, but the weird thing is that it will read in
the last line twice. What's wrong with this?
int main() {
ifstream data;
data.open ("test.txt");
while(data)
{
data>>x>>y>>z;
}
data.close();
return 0;
}
If the file contains
1.2 2.2 3.3
1.3 2.3 4.3
it will reads in
1.2 2.2 3.3
1.3 2.3 4.3
1.3 2.3
b***y
发帖数: 2799
18
☆─────────────────────────────────────☆
shuo (说说罢了) 于 (Thu Sep 22 16:28:10 2005) 提到:
其实是很简单的问题。 两个file,一个file有 node1 node2 C 三个field。另一个
file是个lookup table有 node E F三个field。输出是, node1 node2 C E(node1
) F(node1) E(node2) F(node2). 就是为node1,2找对应的E, F然后全部输出的意思
。 可是我不太会写程序,只能写最简单最笨的。 所以我写乐下边这个。 都还没管node2
, 想着先把node1 得E,F输出出来到一个out.txt里边,然后在运行一遍给node2找对应的E
, F。可是因为我的file1很大,几十万条record。 这个程序运行了2个小时乐,才1/5多
点。 可能也是file I/O的问题。 可是如果我不开关file2, 我不知道怎么让file2的
ifstream指针回到文件最顶上。 结果就是他只查找一遍,然
d****n
发帖数: 130
19
来自主题: Programming版 - 如何把文件内容读到2D的vector里?
在文件里放着一个2D数组数据,想读到一个2D的vector里:
ifstream in("data.dat");
istream_iterator head(in);
istream_iterator tail(head);
vector > data;
for (int i = 0; i < x_size; ++i) {
head = tail;
advance(tail, y_size);
data.push_back(vector());
copy(head, tail, back_inserter(data.back()));
}
不work啊。
B*****y
发帖数: 497
20
来自主题: Programming版 - C++读文本文件怎么判断换行?
文本文件里面每行存的是浮点或者整数,但列数不定
比如
1 2 3.3 5 8
89 12 11 3.3 4.4 56 80 1
34 12
如果用ifstream来做,怎么判断换行并且把读出来的数字存到相应的变量(每行有不同
的变量来存,所以需要判断换行)中?
谢谢!
z****e
发帖数: 2024
21
来自主题: Programming版 - 问一个C++文件读取的问题
int i, k;
//Create a file
ofstream OUTPUT( "RESULT.dat"); //ios::out | ios::in );
if( !OUTPUT ){
cerr << "File could not be opened." << endl;
exit( 1 );
}
//Input the data
for( i=1; i<=8; i++ ){
k = i*10;
cout << " k = " << k << endl;
OUTPUT.write( reinterpret_cast( &k ), sizeof(int) );
}
OUTPUT.close();
cout << endl << "Start Output" << endl;
ifstream INPUT("RESULT.dat");
INPUT.seekg( 2*sizeo
z****e
发帖数: 2024
22
来自主题: Programming版 - istream_iterator问题
读一个文件,
ifstream inf1("test.txt");
istream_iterator isf1(inf1);
istream_iterator eos;
vector v1;
copy(isf1,eos,back_inserter(v1));
发现,文件test.txt读进来的时候,自动被用空格分割,string在vector里边都是原来
文件里,默认空格为dlim的情况。
我没有说明用空格,怎么就默认用空格分割string了?能不能改这个默认的dlim,比如
变成换行?
d****p
发帖数: 685
23
来自主题: Programming版 - istream_iterator问题
Any white char, like space, CR and tab is a delimiter for ifstream. It looks
like difficult to change it though.
If you really want to customize it, you may consider use boost tokenizer.
z****e
发帖数: 2024
24
#include
#include
ifstream inf("your_file_wants_to_be_read.txt");//source file
vector vs((istream_iterator(inf)),
istream_iterator());//read into string vector
for the second sentence, pay attention, do not be fooled by the "most vexing
parse of C++",
else you will only define a function rather than a vector.
h*****0
发帖数: 4889
25
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
前面4字节是BOM吧
k********n
发帖数: 182
26
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
但是实际的文件里没有这四个字节(我用UE打开看的,对不对?)
而且用另外一个工程打开同样的文件,就没有这个问题了
h*****0
发帖数: 4889
27
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
用UE hex模式可以看到吧?
k********n
发帖数: 182
28
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
就是用这个看的,开头没有那四字节。
h*****0
发帖数: 4889
29
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
那4个是什么字符?
z****e
发帖数: 2024
30
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
你确定
char bufz[1024];
memset(bufz, 0, sizeof(bufz));
cout< 如上三句话紧挨着,还给你个“1”?
k********n
发帖数: 182
31
来自主题: Programming版 - 一个很诡异的ifstream问题,求助~~
是我搞错了,原因是我在release模式下debug了。。
D******4
发帖数: 47
32
来自主题: Programming版 - 请问一个入门级 dynamic memory 的问题
今天看到dynamic memory,测试如下程序:
#include
#include
#include
using namespace std;
ifstream fin("test.in");
ofstream fout("test.out");
int main (){
int i = 0;
int n = 0;
int* p;

if(fin.is_open()){
fin>>n;
fout<<"n is: "< p = new (nothrow) int[n];
if (p == 0)
fout << "Error: memory could not be allocated"< else{
for (i=0; i> p[i];
fin.close();
fout << "You have ent
h*****4
发帖数: 4219
33
来自主题: Programming版 - 如何让这个cur变量正确指向新地址
本人刚学数据结构,有个问题没找到解决方法,还请各位大牛帮忙看下。具体情况如下:
const int BUFFERSIZE=512;
struct TreeItem {
KeyType teamName;
int gameWins;
};
TreePtr insertItem(TreePtr root, const TreeItem newItem){
if(root==NULL)
root=new TreeNode(newItem,NULL,NULL);
else if(strcmp(newItem.teamName,root->item.teamName)<0)
insertItem(root->left,newItem);
else insertItem(root->right,newItem);
return root;
}
TreePtr fileInit(ifstream & db_input)
{ char buf[BUFFERSIZE];
char *bufpt... 阅读全帖
H***a
发帖数: 735
34
来自主题: Programming版 - how to use cin as default ifstream?
cin is istream, just use it.
H***a
发帖数: 735
35
来自主题: Programming版 - how to skip the last empty lines in ifstream?
The EOF is a bit tricky. It's easy to remember that:
** always check right after you attempt to read **
There are two solutions:
1)
...
if ( infile.is_open() )
{
string str("");
getline(infile, str); //read it first!!
while ( infile.good() )
{
lines.push_back(str);
getline( infile, str ); //now get the next line!
}
infile.close();
}
...
2) Simpler way which is recommended
...
if ( infile.is_open() )
{
string str("");
while ( getline(infile, ... 阅读全帖
g*********s
发帖数: 1782
36
来自主题: Programming版 - how to skip the last empty lines in ifstream?
thx. i like your solution.
but could you point out where the extra line comes from in my code? i just
want to have a deeper understanding on c++ io.
H***a
发帖数: 735
37
来自主题: Programming版 - how to skip the last empty lines in ifstream?
Np. My understanding is that (for your original code):
eof() or good() checks internal state flags "eofbit", which is modified when
getline() is encountered problem in reading from infile.
When getline() reads the last line, it stops by seeing the EOF, however it
consider this read is successful so won't flip eofbit to TRUE. The file
pointer moves to position of EOF (remember getline() returns istream&)
Since eofbit is still FALSE now, eof() or good() lets it loop over, getline(
) sees the EOF a... 阅读全帖
t****t
发帖数: 6806
38
no, most ppl don't need that. just know istream and ostream and their
variations is enough (istream is basic_istream, similar for ostream).
their variations include ifstream/ofstream, istringstream/ostringstream.
just know op<< for output, op>> for input, op! (usually you need that
instead of eof() member) to check eof, clear() for opening another file,
manipulators (such as setw(), endl, hex, etc.) for formating, and getline()
for reading a whole line. these are enough for 90% of ppl deal... 阅读全帖
c**********e
发帖数: 2007
39
来自主题: Programming版 - C++ Q 99-102 (转载)
【 以下文字转载自 JobHunting 讨论区 】
发信人: careerchange (Stupid), 信区: JobHunting
标 题: C++ Q 99-102
发信站: BBS 未名空间站 (Fri Oct 28 23:14:06 2011, 美东)
C++ Q 99: global and static
What type of linkage does global variables and functions preceded by
the storage class specifier static have?
a. Internal
b. Intern
c. External
d. Extern
Answer: a
C++ Q 100: directives
Any number of which of the following directives can appear between
the #if and #endif directives?
a. #elif
b. #endif
c. #else
d. #if
Answer: a
C++ Q101... 阅读全帖
y**b
发帖数: 10166
40
使用流迭代器是不是会快一些?通常一个一个push进容器的效率不如给出一个迭代器范
围。
ifstream in("your_file");
istream_iterator in_iter(in);
istream_iterator eof;
priority_queue pq(in_iter, eof);
当然your_class需要重载输入操作符>>

varialbe
f*****Q
发帖数: 1912
41
re
int length;
char * fileBuffer;
ifstream is;
is.open ("xxx.csv", ios::binary );
is.seekg (0, ios::end);
length = is.tellg();
buffer = new char [length];
is.seekg (0, ios::beg);
is.read (fileBuffer,length);
然后就对着fileBuffer干好了。
k*******3
发帖数: 1909
42
我在windows cmd下做了
type database.txt >> CGIMetPred.exe
然后把tool.exe复制到另一个文件夹,但是tool.exe运行说找不到database.txt文件
tool的cpp原程序中有一句
ifstream infile("database.txt");
如果找不到database.txt就会报错。
请问我应该如何修改?谢谢
k*******3
发帖数: 1909
43
就是说原程序中不能用
ifstream infile("database.txt");
来读取文件了对吗?
要改用指针?
谢谢
G*****9
发帖数: 3225
44
来自主题: Programming版 - Urgent question
I used C++ to create a *.SO file, in which I read double values from text
files (ifstream>>). However, the speed is incredibly slow. The same code, if
compiled as .out, is 40 times faster. Can anyone tell me why?
I am using AIX and g++. Thanks.
d**********u
发帖数: 3371
45
来自主题: Programming版 - fstream不能做类的成员吗
看了一下 好想只能用
std::ifstream *fs;
作为类成员 否则报错
是什么原因呢
m**u
发帖数: 632
46
来自主题: Unix版 - 请问:装了GCC以后
gcc可以编译了,但我一些C程序中用到几个C++的流,如ifstream就
找不到了,该怎么设置呢.原来在系里solaris上是可以的,现在用
自己的solaris就不知道该怎么办了
谢谢
m**u
发帖数: 632
47
来自主题: Unix版 - Re: [转载] 请问:装了GCC以后
【 以下文字转载自 Programming 讨论区 】
【 原文由 mliu 所发表 】
果然如此
可是iostream 和ifstream必须带.h啊,否则还是通不过
另外编译出来的文件执行出错:
ld.so.1: xxx(excutable name) :fatal: libstdc++.so.5: open
failed: No such file or directory
Killed
这是什么问题呢,为什么编译出来还不能执行呢
谢谢
M**********n
发帖数: 432
48
How to read binary(data) file generated by Fortran in C/C++?
Is it possible? If possible how to realize it?
The binary file generated by Fortran is a double precision data file.
I have tried to use read binary file in C++, but have problem. The following
is my C++
code.
double temp=0.0;
ifstream File("bin.data",ios::in|ios::binary);
File.read((char *) &temp, sizeof(double));
cout< The number that has been read out does not make any sense.
Anybody know why? Thanks.
s*********g
发帖数: 10
49
查一查fstream, ifstream这两个头文件吧
w********0
发帖数: 1211
50
【 以下文字转载自 Quant 讨论区 】
发信人: waiting140 (等待140), 信区: Quant
标 题: 求助:C++里的fstream究竟该怎么用?
发信站: BBS 未名空间站 (Sat Mar 5 12:10:26 2011, 美东)
今天想上机实践一下读写文件,最简短不过的小程序了,但是很失败。哪位有经验的帮
忙看一下,先谢过了。
我header file 里是这么写的:
#include
#include
#include
using std::cin;
using std::cout;
using std::string;
source file 里是这么写的:
#include "march_05_2011.h"
int main(){
string s;
s = "hello world! \n";
cout << s;
fstream testfile;
return 0;
}
可编译起来死活不认那个fstream, 改成ifstream, ofstream都没用。
如果把fstream... 阅读全帖
首页 上页 1 2 (共2页)