l******0 发帖数: 313 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: love1010 (学会move on), 信区: Programming
标 题: C的fscanf的问题
发信站: BBS 未名空间站 (Sat Aug 18 00:31:27 2012, 美东)
需要从一个文件里读数据写入一些变量里
文件是这样的格式 GOOG|588.88
需要将GOOG写入一个string里,然后将588.88写入一个float number里,我写了以下
code:
char tempprice[10];
char ticker[10];
fscanf(fr,"%[^|]|%[^\n]\n",ticker,tempprice);
float stockprice = stof(tempprice); //将string变成float
上面的code, ticker会正确得到GOOG,但是tempprice却无法得到588.88,请问哪里出
错了?
另外,有更优化的方法么
非常感谢 |
|
b******y 发帖数: 2729 | 2 【 以下文字转载自 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;
哪一种方法更好更实用?谢谢! |
|
b******y 发帖数: 2729 | 3 【 以下文字转载自 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;
哪一种方法更好更实用?谢谢! |
|
l********a 发帖数: 1154 | 4 你能保证|前面的长度吗?如果都是4,下面的可以
int main()
{
FILE *fid = fopen("data.txt","r");
float num = 0.0;
char name[10] = {0};
fscanf(fid,"%4c|%f",&name,&num);
fclose(fid);
printf("Name: %s\nNumber: %f\n",name,num);
return 0;
}
如果分隔符不是|而是空格,用
fscanf(fid,"%s %f",&name,&num)好使
%s String of characters. This will read subsequent characters until a
whitespace is found (whitespace characters are considered to be blank,
newline and tab). |
|
f****p 发帖数: 18483 | 5 fscanf需要地址作为参数,那个ticker本来就是字符串地址,所以没事,588.88应该直
接定义一个浮点数变量,然后用&取变量地址。fscanf自动进行转换。 |
|
S***a 发帖数: 116 | 6 比如我有个文件里面数据的格式是 1.234e+008.
也就是用fprintf的%10.3e格式输出去的.
可是用同样的参数在fscanf中怎么死活就读取不回来乐呢?
难道只能%f输出,然后%f读取才行? |
|
l******0 发帖数: 313 | 7 需要从一个文件里读数据写入一些变量里
文件是这样的格式 GOOG|588.88
需要将GOOG写入一个string里,然后将588.88写入一个float number里,我写了以下
code:
char tempprice[10];
char ticker[10];
fscanf(fr,"%[^|]|%[^\n]\n",ticker,tempprice);
float stockprice = stof(tempprice); //将string变成float
上面的code, ticker会正确得到GOOG,但是tempprice却无法得到588.88,请问哪里出
错了?
另外,有更优化的方法么
非常感谢 |
|
d*******2 发帖数: 340 | 8 FILE *pFile;
pFile=fopen("myfile.txt","r");
fscanf(pFile,"%e, %e, %e, %e",&variable1,&variable2,&variable3,&variable4)
fclose(pFile);
myfile.txt
4.232e-002, 4.232e-002, 4.232e-002, 4.232e-002
为什么不对呢?
先谢了! |
|
|
c******d 发帖数: 906 | 10 请教一下如果使用instrument toolbox
目的是使用matlab去登陆 本地邮件服务器
t=tcpip('localhost',110);
fopen(t);
fscanf(t) % 这里返回 +OK POP3
fprintf(t,'USER xxx') % 到这里就不行了,不能输入任何字符
fscanf(t)
% 返回 "A timeout occurred before the Terminator was reached. "
fprintf(t,'PASS xxx');
fscanf(t)
% 同样返回 "A timeout occurred before the Terminator was reached. "
fclose(t);
delete(t);
clear t; |
|
j**u 发帖数: 6059 | 11 ☆─────────────────────────────────────☆
eagletiger (eagletiger) 于 (Wed Nov 23 23:01:21 2011, 美东) 提到:
程序里现在需要一个很大的Matrix, 以前写的时候是放在一个文件里,每次程序执行的
时候现读入,这样很浪费时间,现在把整个Matrix写成一个array存在程序里作global
variable,但是编译的时候很慢,要十几分钟的样子,而且编译出来的程序很大,请问
各位一般处理大的矩阵时候是如何处理的,谢谢哈.
☆─────────────────────────────────────☆
Augu (奥古) 于 (Thu Nov 24 16:35:31 2011, 美东) 提到:
要不空间换时间
要不时间换空间
global
☆─────────────────────────────────────☆
eagletiger (eagletiger) 于 (Fri Nov 25 02:14:55 2011, 美东) 提到:
俺现在就是空间换时间,矩阵大概... 阅读全帖 |
|
x******a 发帖数: 6336 | 12 I got thousands problems on the following piece of code "dumpfile.h" when I
compile under cygwin. it is ok under visual stduio... can anyone help?
Thanks!
#include
#include
#include //ostream_iterator
#include //cerr
#include //std::copy
template
void dump_to_file(const char* filename, const std::vector& v_d){
std::ofstream ofs(filename);
if(!ofs){
std::cerr<<"Unable to open the file to write!n";
return ;... 阅读全帖 |
|
b****e 发帖数: 11 | 13 我碰到一个十分奇怪的问题,我需要在linux下读取pgm图象文件,
一般这种文件,前面三行是文本的,用ultraedit就可以看出它
的内容,
####################PGM FILE#####################
P5
width height
maximum
............(binary data of the graylevel image)
结果同样的代码,在linux下读得的结果跟windows下读得的结果有一点区别
大部分象素都是一样的,因为我的图象是一个mask,只有黑白之分(0/255)
结果在linux下读写的总是在图象边缘上无辜多出一些白色线段,而且只在
图象边缘处有这个现象,这些地方本来是背景色黑色的。windows下读就没有
这个问题
另外我还想问一个比如我读到上面所示文件的最后一行用
fscanf(fp,"%d", &max)
和
fscanf(fp,"%d\n",&max)
有区别吗,后面的图象象素都是用fread读取,文件用"rb"方式打开
我发现两种方法独得的象素值大部分是一样的,但是在某些地方又
不一样,搞不明白。 |
|
l******o 发帖数: 18 | 14 编译程序通过,但是运行的时候出现如题错误
我用dbx debug, 结果说 signal SEGV (no mapping at the fault address) in fscanf
at XXXX
出错的一行是个很简单的读入语句 fscanf(velfp, "%d", &npt);
实在不知道什么意思
还有,我的这个程序是对每个事件分网格计算,当把网格分的很大的时候,整个程序运行没
问题
分的小一点就在中间出错. 可是出错之前,又已经有事件计算完毕了. 对每个事件不过循
环
而已,参数有些改变
实在找不出哪儿的错,郁闷 |
|
A**l 发帖数: 2650 | 15 A sequence of white-space characters (space, tab, newline, etc.; see isspace
(3)). This directive matches any amount of white space, including none, in
the input.
I have no idea what "[^\n]" exactly means... But you can use %s for sure. |
|
c****p 发帖数: 6474 | 16 "%s|%f", ticker, price? |
|
l****c 发帖数: 838 | 17 You should read in the string and parse.
You don't know how long the first part string is or how long the number is.
So if you define:
char tempprice[10];
char ticker[10];
You have the risk of buffer overflow.
Here is my solution. I debug it as I wrote it, so it is not optimized.
it is pure C. You can get result with 2 lines of perl or python code
============================
#include
#include
#include
int main()
{
char *str = "GOOD|89.34";
char *ptoken, *... 阅读全帖 |
|
|
f******e 发帖数: 921 | 19 I have a perl code, suppose the code file is "H.bg", I will run the code
like this way: ../Perl2/BNG2.pl H.bg, then it will output a data file
called: H.data.
Since I want to run it 10 times automatically and take one row of data
out. So, I want to use Matlab to implement it,
for i=1:10
../Perl2/BNG2.pl H.bg
fid=fopen('H.data','r');
a = fscanf (fid, '%f');
A(i)=a(size(a)-4:size(a));
clear a;
end
when I run, it said wrong in 2nd row. Could someone help me? I am not good
at Matlab.Thanks |
|
c*****g 发帖数: 119 | 20 like fscanf("%d %d", &a, &b) in C? |
|
N****w 发帖数: 21578 | 21 那就随便你怎么搞了
不想都放内存里,就读两遍吧
第一遍把每行在什么位置、长度记下来,也可以不用读,光用 fscanf 找行尾
再读一遍的时候,算出每行该写到目标文件的什么位置,写之 |
|
G**Y 发帖数: 33224 | 22 What "%[^\n]\n" means?!
If the first few lines of an input file are:
hello
hello
world
...
the 3rd line is empty, no extra spaces at the end of the lines. What the output should be?
#include
#include
int main(void) {
FILE* fp;
char file[999]="hello.world";
char dummy1[9999], dummy2[9999], dummy3[9999], dummy4[9999];
int line_max=9999;
int i;
if ((fp = fopen(file, "r")) == NULL) {
printf("Cannot open file %s for reading. \n", file);
exit( |
|
d****i 发帖数: 4809 | 23 这个应该是用指针来定义动态数组吧,如下
struct Numbers {
size_t N;
float *data;
};
struct Numbers numbers;
//read N in
scanf("%d", &numbers.N);
//create the new data array based on N
number.data = malloc(sizeof(float)*N);
//read data in
for(int i=0; i
fscanf(pFile, "%f", &number.data[i]);
}
float) |
|
l***e 发帖数: 480 | 24 PPM的图形文件,P3字符格式和P6二进制格式。
怎么读?
对二进制格式,GETC读不进来。FREAD读到数组里,好象也不对。不知为何。
字符格式每行三个数字字符,以空格分隔:XXX XXX XXX
FSCANF转换好象有问题。
哪位有经验,介绍一点。多谢 |
|
h***s 发帖数: 2499 | 25 fscanf好像是一行一读的,一个简单的方法是%s %s, a, b. |
|
X****r 发帖数: 3557 | 26 以下程序没测试过,仅供参考。
/**
* @param input The input file.
* @param size A pointer to store result size.
* @return The pointer to the result data,
* or NULL for not enough memory.
*/
int *read_all(FILE *input, int *size) {
int n = 128, i;
int *data = (int *)malloc(n * sizeof(int));
for (i = 0; data && fscanf(input, "%d", data + i); i++) {
if (i == n) {
data = (int *)realloc(data, (n *= 2) * sizeof(int));
}
}
if (size) {
*size = i;
}
return data;
} |
|
f******e 发帖数: 921 | 27 I have a perl code, suppose the code file is "H.bg", I will run the code
like this way: ../Perl2/BNG2.pl H.bg, then it will output a data file
called: H.data.
Since I want to run it 10 times automatically and take one row of data
out. So, I want to use Matlab to implement it,
for i=1:10
../Perl2/BNG2.pl H.bg
fid=fopen('H.data','r');
a = fscanf (fid, '%f');
A(i)=a(size(a)-4:size(a));
clear a;
end
when I run, it said wrong in 2nd row. Could someone help me? I am not good
at Matlab.Thanks |
|
s********1 发帖数: 581 | 28 有一个text file input.txt,内容只有一行,如下:
12345 |
|
t****t 发帖数: 6806 | 29 so who told you "12345" is an int with 4 bytes? |
|
s********1 发帖数: 581 | 30 I wrote it into input.txt as an int. Also it's within the int range. what
else could it be? |
|
t****t 发帖数: 6806 | 31 according to your logic, it's also within the 16bit short range. why it is
not a 16bit short?
it's also with a 64bit long long range. why it is not a 64bit long long? |
|
s********1 发帖数: 581 | 32 I wrote it into the file as an int not as other data format. |
|
t****t 发帖数: 6806 | 33 if you write it into the file "as a short", does it make a difference?
most probably, it doesn't. therefore you didn't write it into the file "as
an int". |
|
|
|
p***o 发帖数: 1252 | 36 FILE *fp = popen("gzip -cd your_file", "r");
Then do whatever you want to do as if it is plaintext, e.g. fgets and fscanf |
|
X****r 发帖数: 3557 | 37 Yes, I forgot about popen, hehe :)
fscanf |
|
c**b 发帖数: 2999 | 38 txt file,then use fscanf |
|
B**F 发帖数: 38 | 39 太初级了只好披马甲来问。
如下程序,如何把example文件中的数字位数一位不少地读入?
试了lf不管用:
0, -0.999714 0.000734634
1, -0.998492 0.00170939
2, -0.996295 0.00268393
多谢指教了!(以下代码中“n"显示不出来)
gfortran, Mac OS X 10.6.7
#include
void main(void)
{
double Z1[3], Z2[3];
int i;
FILE *input= fopen ("example", "r");
for (i=0; i<3; ++i) {
fscanf(input, "%lf %lfn", &Z1[i], &Z2[i]);
printf("%i, %g %gn", i, Z1[i], Z2[i]);}
fclose(input);
return 0;
}
文件"example"内容:
-0.99971372677344128 ... 阅读全帖 |
|
l********a 发帖数: 1154 | 40 这个只能fopen,fscanf,fclose按照格式读取了 |
|
l******0 发帖数: 313 | 41 你好,谢谢,不能保证都是4,但可以保证长度小于6 |
|
b***i 发帖数: 3043 | 42 不就是读入直到|,然后读入|到一个char里面,然后读入一个浮点数吗
int main(){
char line[]={"GOODG|256.5"};
char name[10];
float x;
char c;
sscanf(line, "%[^|]%c%f", name, &c, &x);
printf("%s\n", name);
printf("%f", x);
return 0;
} |
|
m*******l 发帖数: 12782 | 43 c++ can do a better job |
|
|
T*******e 发帖数: 47 | 45 check help of fopen, fclose, fread, fscanf, and fprintf commands.
to run an .exe file, check ! (use 'help punct') |
|
|
d*******2 发帖数: 340 | 47 没有error message. 用
cout << "variable1: " << variable1 << endl;
cout << "variable2: " << variable2 << endl;
看,
variable1: 1
variable2: 7.7433e+268
variable1,variable2是double型. (double variable1,variable2;) |
|
l*****d 发帖数: 754 | 48 Google fopen C++, and see the parameter list of fopen function. |
|
l*****d 发帖数: 754 | 49 En, you changed your original post, but that is ok.
Please post the whole source code of the smallest complete program as you
found problem. |
|
d***y 发帖数: 65 | 50 maybe you can try to use "%lf" instead of "%e" |
|