1-2-3-4-5-6-7
MAX=200
要求第一行直接跳过,然后第二行的“ MAX=”也跳过,直接把200这个 整数读出来
24 个解决方案
#1
读一行fgets,
然后用fscanf读或fgets后自己解析。
然后用fscanf读或fgets后自己解析。
#2
ifstream fin("data.txt")
用fin怎么搞?
用fin怎么搞?
#3
应该说用ifstream相关的东西怎么搞?
#4
如果是读取文件,是跳不过去的。文件在磁盘上就是二进制数据,没有行的概念,无法直接定位到某一行。所以只能先读取一定字节数的文件,再跳过你不要的行。比如先将全文读到一个串里面,再用getline分行读取,忽略第一行。。。
#5
C语言库中有相关函数,楼主查查,我记不得了
#6
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
if ( strstr(s, "MAX=") )
{
}
}
#7
可以一行行的读 然后在取你要的字节~ 不能直接跳到你说的位置
#8
ifstream fin("data.txt");
string s;
while( getline(fin,s) )//逐行读取到s中去
{
if ( strstr(s, "MAX=") )
{
string b = s.substr(4,strlen(s)+1);
}
}
#9
FILE *f;
int MAXV;
f=fopen(...,"rb");
fseek(f,19,SEEK_SET);
fscanf("%d",&MAXV);
fclose(f);
//1-2-3-4-5-6-7(连行尾的'\r\n'共15个字节)
//MAX=200(MAX=共4字节)
//所以fseek跳到第15+4==19字节
#10
上帖中代码
fscanf("%d",&MAXV);
应改为
fscanf( f,"%d",&MAXV);
fscanf("%d",&MAXV);
应改为
fscanf( f,"%d",&MAXV);
#11
第一行长度不定的~~~
第二行MAX=是固定的
#12
好像czh3642210的是个办法,问题是还有转换成整数,,,
我看了一下文档,seekg能完成吗?
我看了一下文档,seekg能完成吗?
operator>> Extract formatted data (public member function )
gcount Get number of characters extracted by last unformatted input operation (public member function)
get Get unformatted data from stream (public member function )
getline Get line from stream (public member function )
ignore Extract and discard characters (public member functions)
peek Peek next character (public member function )
read Read block of data (public member function)
readsome Read block of data available in the buffer (public member function )
putback Put character back (public member function )
unget Decrement get pointer (public member function )
tellg Get position of the get pointer. (public member function )
seekg Set position of the get pointer (public member function )
sync Synchronize input buffer with source of characters (public member function)
sentry Perform exception safe prefix/suffix operations (public member class)
#13
char buf[50]={0};
int num;
FILE *fp=fopen("test.txt","r");
if(fp)
{
while(1)
{
memset(buf,0,50);
if(!fgets(buf,50,fp))
break;
if(strstr(buf,"-"))
continue;
sscanf(buf,"%*[^=]=%d",&num);
}
fclose(fp);
}
printf("%d",num);
return 0;
#14
奇怪了!
我这样写能编译通过:
ifstream fin("data.txt");
string s;
getline(fin ,s);
但是这样写 就没法通过:
ifstream fin("data.txt");
string s;
fin.getline(s);
#15
fin.getline(s);
没这种用法,参数个数不匹配
没这种用法,参数个数不匹配
#16
楼主可以都读,然后判断是否包含MAX=,如果是,然后取该行的后几个字符,或者去掉MAX=
#17
fin.getline(..)和getline(fin,..)不一样么?
#18
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
using std::cout;
int main(void)
{
ifstream fin("text");
string s;
char c[20];
char *p=0;
while( getline(fin,s) )
{
p=strcpy(c,s.c_str());
}
string sentence("MAX=");
for(string::size_type i=0;i!=sentence.size();p++,i++){}
cout<<p;
return 0;
}
#19
预留一块足够的内存
fgets读入,不处理
fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
fgets读入,不处理
fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
#20
引用 19 楼 cuiy0001 的回复:
fgetc直到读出'\n'(当然如果先读到EOF就异常退出)
再fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
fgetc直到读出'\n'(当然如果先读到EOF就异常退出)
再fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
#21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
int a[10]={0}, x;
string s;
ifstream fin("data.txt");
int n=0;
while (fin >> x) {
a[n++]=x;
fin.get();
}
getline(fin,s);
fin.ignore(4,' ');
fin >> a[n];
for (int i=0; i<=n; i++)
cout << a[i] << endl;
system("pause");
return 0;
}
data.txt:
1-2-3-4
MAX=200
这样子怎么读不到所有整数?
只有1、2、3、4四个整数,怎么没有200?
#22
21楼的问题,求助啊!
#23
摒弃fstream;使用FILE *!
//读取data.txt中能读取的所有整数
#include <stdio.h>
int n,r;
int d;
FILE *f;
void main() {
f=fopen("data.txt","r");
if (NULL==f) {printf("Can not open file data.txt!\n");return;}
n=0;
while (1) {
r=fscanf(f,"%d",&d);
if (1==r) {
n++;
printf("[%d]==%d\n",n,d);
} else if (0==r) {
fgetc(f);
} else break;
}
fclose(f);
}
#24
好吧~~~为啥要摒弃fstream? 我是c/c++菜鸟,相关经验很少
#1
读一行fgets,
然后用fscanf读或fgets后自己解析。
然后用fscanf读或fgets后自己解析。
#2
ifstream fin("data.txt")
用fin怎么搞?
用fin怎么搞?
#3
应该说用ifstream相关的东西怎么搞?
#4
如果是读取文件,是跳不过去的。文件在磁盘上就是二进制数据,没有行的概念,无法直接定位到某一行。所以只能先读取一定字节数的文件,再跳过你不要的行。比如先将全文读到一个串里面,再用getline分行读取,忽略第一行。。。
#5
C语言库中有相关函数,楼主查查,我记不得了
#6
ifstream fin("data.txt");
string s;
while( getline(fin,s) )
{
if ( strstr(s, "MAX=") )
{
}
}
#7
可以一行行的读 然后在取你要的字节~ 不能直接跳到你说的位置
#8
ifstream fin("data.txt");
string s;
while( getline(fin,s) )//逐行读取到s中去
{
if ( strstr(s, "MAX=") )
{
string b = s.substr(4,strlen(s)+1);
}
}
#9
FILE *f;
int MAXV;
f=fopen(...,"rb");
fseek(f,19,SEEK_SET);
fscanf("%d",&MAXV);
fclose(f);
//1-2-3-4-5-6-7(连行尾的'\r\n'共15个字节)
//MAX=200(MAX=共4字节)
//所以fseek跳到第15+4==19字节
#10
上帖中代码
fscanf("%d",&MAXV);
应改为
fscanf( f,"%d",&MAXV);
fscanf("%d",&MAXV);
应改为
fscanf( f,"%d",&MAXV);
#11
第一行长度不定的~~~
第二行MAX=是固定的
#12
好像czh3642210的是个办法,问题是还有转换成整数,,,
我看了一下文档,seekg能完成吗?
我看了一下文档,seekg能完成吗?
operator>> Extract formatted data (public member function )
gcount Get number of characters extracted by last unformatted input operation (public member function)
get Get unformatted data from stream (public member function )
getline Get line from stream (public member function )
ignore Extract and discard characters (public member functions)
peek Peek next character (public member function )
read Read block of data (public member function)
readsome Read block of data available in the buffer (public member function )
putback Put character back (public member function )
unget Decrement get pointer (public member function )
tellg Get position of the get pointer. (public member function )
seekg Set position of the get pointer (public member function )
sync Synchronize input buffer with source of characters (public member function)
sentry Perform exception safe prefix/suffix operations (public member class)
#13
char buf[50]={0};
int num;
FILE *fp=fopen("test.txt","r");
if(fp)
{
while(1)
{
memset(buf,0,50);
if(!fgets(buf,50,fp))
break;
if(strstr(buf,"-"))
continue;
sscanf(buf,"%*[^=]=%d",&num);
}
fclose(fp);
}
printf("%d",num);
return 0;
#14
奇怪了!
我这样写能编译通过:
ifstream fin("data.txt");
string s;
getline(fin ,s);
但是这样写 就没法通过:
ifstream fin("data.txt");
string s;
fin.getline(s);
#15
fin.getline(s);
没这种用法,参数个数不匹配
没这种用法,参数个数不匹配
#16
楼主可以都读,然后判断是否包含MAX=,如果是,然后取该行的后几个字符,或者去掉MAX=
#17
fin.getline(..)和getline(fin,..)不一样么?
#18
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
using std::cout;
int main(void)
{
ifstream fin("text");
string s;
char c[20];
char *p=0;
while( getline(fin,s) )
{
p=strcpy(c,s.c_str());
}
string sentence("MAX=");
for(string::size_type i=0;i!=sentence.size();p++,i++){}
cout<<p;
return 0;
}
#19
预留一块足够的内存
fgets读入,不处理
fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
fgets读入,不处理
fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
#20
引用 19 楼 cuiy0001 的回复:
fgetc直到读出'\n'(当然如果先读到EOF就异常退出)
再fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
fgetc直到读出'\n'(当然如果先读到EOF就异常退出)
再fseek(fp,4,SEEK_CUR);
最后 fscanf读取整数
#21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
int a[10]={0}, x;
string s;
ifstream fin("data.txt");
int n=0;
while (fin >> x) {
a[n++]=x;
fin.get();
}
getline(fin,s);
fin.ignore(4,' ');
fin >> a[n];
for (int i=0; i<=n; i++)
cout << a[i] << endl;
system("pause");
return 0;
}
data.txt:
1-2-3-4
MAX=200
这样子怎么读不到所有整数?
只有1、2、3、4四个整数,怎么没有200?
#22
21楼的问题,求助啊!
#23
摒弃fstream;使用FILE *!
//读取data.txt中能读取的所有整数
#include <stdio.h>
int n,r;
int d;
FILE *f;
void main() {
f=fopen("data.txt","r");
if (NULL==f) {printf("Can not open file data.txt!\n");return;}
n=0;
while (1) {
r=fscanf(f,"%d",&d);
if (1==r) {
n++;
printf("[%d]==%d\n",n,d);
} else if (0==r) {
fgetc(f);
} else break;
}
fclose(f);
}
#24
好吧~~~为啥要摒弃fstream? 我是c/c++菜鸟,相关经验很少