
1、用fgets函数可以读取文件中某行的数据,某列数据就必须一个一个读入每行的第几个字符,再存入到一个字符串当中。
2、例程:
#include<stdio.h>#include<string.h>
void main()
{
char a[100],b[100],c[100]
int i=3,j=4,k=0 //第三行,第四列
FILE *fp = fopen("data.txt","r")
while(fgets(c,100,fp)){ //读入每行数据
i--
if(i==0) strcpy(a,c) //读到第三行数据
b[k++]=c[j-1] //把每行的那列字符拷到b中
}
b[k]=0
printf("第%d行数据:%s\n",i,a)
printf("第%d列数据:%s\n",j,b)
fclose(fp)
}
private void Form1_Load(object sender, EventArgs e){
int j=0
label1.Text= FileRowText(@"c:\update.dat",2, ref j)
}
#region 获取文本某行数据
///
/// 获取文本文件某行数据
///
/// 文本文件路径
/// 第几行
/// 返回总行数
///
private string FileRowText(string filePath,int i, ref int j)
{
StreamReader sr = File.OpenText(filePath)
string str = ""
string text = ""
int num = 0
while ((str = sr.ReadLine()) != null)
{
num++
if (num == i)
{
text = str
}
}
j = num
sr.Close()
return text
}
#endregion
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)