社区
C语言
帖子详情
懂文件读写的高手进来看看
cplusplusman
2003-12-14 11:35:19
有这样一个文本文档:
1 3
3 4
4 6
##
想读取文件中的数据,一行为一单元,直到有##停止,应该用什么读写函数比较好,提示点思路,1 3后面那么多空格才到3 4应该怎么做读到呢?我比较菜,请各位给点详细的解释,不慎感激。
...全文
159
7
打赏
收藏
懂文件读写的高手进来看看
有这样一个文本文档: 1 3 3 4 4 6 ## 想读取文件中的数据,一行为一单元,直到有##停止,应该用什么读写函数比较好,提示点思路,1 3后面那么多空格才到3 4应该怎么做读到呢?我比较菜,请各位给点详细的解释,不慎感激。
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
junnyfeng
2003-12-14
打赏
举报
回复
把老徐的改良一下,会运行得更好
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
long a, b;
char astr[256], bstr[256];
FILE * fp;
fp = fopen("test.txt", "r");
while (!feof(fp))
{
fscanf(fp, "%s %s", astr, bstr);
if (strcmp(astr, "##")==0)
break;
a = atoi(astr);
b = atoi(bstr);
if(strcmp(bstr, "##")==0)
{
printf("%d",a);
break;
}
printf("%d, %d, ", a, b);
}
fclose(fp);
}
i_jianyong
2003-12-14
打赏
举报
回复
// C++ 版本
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs("data.txt);
string s1, s2;
while (ifstream >> s1 >> s2)
{
if (s1 == "#" && s2 == "#")
break;
cout << s1 << ' ' << s2 << endl;
}
}
junnyfeng
2003-12-14
打赏
举报
回复
你怎么说后面有很多空格呢?都是你放的啊? 说不定是个回车呢。
如果内容不太规则,可以用fgetc()
如果只是针对你上面的结构,可以用如下的程序
#include <stdio.h>
void main()
{
FILE *fp;
char a,b;
fp=fopen("test.txt","r");// 假定文本为test.txt
do{
fscanf(fp,"%c%c",&a,&b); // 读文件
printf("%c%c",a,b);
}while(a!='#' && b!='#');
}
smalltalk
2003-12-14
打赏
举报
回复
int a, b;
char astr[256], bstr[256];
FILE * fp;
fp = fopen("filename.txt", "r")
while (!feof(fp))
{
fscanf(fp, "%s %s", astr, bstr);
if (strcmp(astr, "#")== 0)
break;
a = atoi(astr);
b = atoi(bstr);
printf("%d, %d", a, b);
}
fclose(fp);
KingI
2003-12-14
打赏
举报
回复
把 junnyfeng(阳光少年) 的稍稍修改了一下
#include <stdio.h>
void main()
{
FILE *fp;
char a,b;
fp=fopen("test.txt","r");
fscanf(fp,"%c%c",&a,&b);
while (a!='#' && b!='#')
{
printf("%c%c",a,b);
fscanf(fp,"%c%c",&a,&b);
}
}
koskinen
2003-12-14
打赏
举报
回复
#include<fstream>
#include<iostream>
#include<vector>
#include<sting>
using namespace std;
void main()
{
ifstream in("你的文件");
string line;
vector<string> lines; //string类容器;
while(getline(in,line)&&line!="##")
{
lines.push_back(line);
}
//这里可以添加你自己的代码;比如输出到屏幕
}
zhangfjj
2003-12-14
打赏
举报
回复
用fscanf函数呀
linux ip获取mac,linux 获取本机MAC/IP地址的方法
功能:查询本机IP/MAC地址,过滤掉127.0.0.1 loop-back 地址适用:linux, ubuntu 16.04 调试通过#include #include #include #include #include #include #include #include #include #include #include #include #include #include #inclu...
linux 获取本机mac地址命令,linux 获取本机MAC/IP地址的方法
功能:查询本机IP/MAC地址,过滤掉127.0.0.1 loop-back 地址适用:linux, ubuntu 16.04 调试通过#include #include #include #include #include #include #include #include #include #include #include #include #include #include #inclu...
Linux内核 获取本机mac,linux 获取本机MAC/IP地址的方法
功能:查询本机IP/MAC地址,过滤掉127.0.0.1 loop-back 地址适用:linux, ubuntu 16.04 调试通过#include #include #include #include #include #include #include #include #include #include #include #include #include #include #inclu...
linux 获取本机MAC/IP地址的方法
功能:查询本机IP/MAC地址,过滤掉127.0.0.1 loop-back 地址适用:linux, ubuntu 16.04 调试通过#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <errno.h&g...
Java 获取IP/MAC地址等网络信息的方法
Java 获取IP/MAC地址等网络信息的方法 1.引入Java包 import java.net.InetAddress; import java.net.NetworkInterface; 2.1获取IP地址的函数 public static String getLocalIP() throws UnknownHostException{ InetAddress
C语言
70,040
社区成员
243,245
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章