当前位置:首页 » 操作系统 » linux读取txt

linux读取txt

发布时间: 2022-02-12 03:36:34

⑴ 用linux下的c语言读取txt文件中的列数据

1.用fgets函数可以读取文件中某行的数据,某列数据就必须一个一个读入每行的第几个字符,再存入到一个字符串当中。

2.例程:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=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 ",i,a);
printf("第%d列数据:%s ",j,b);
fclose(fp);
}

⑵ 怎样用 java读取txt文件中的数据 linux下

try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("文件路径")));
for (String line = br.readLine(); line != null; line = br.readLine()) {
System.out.println(Integer.parseInt(line));
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}

⑶ linux终端如何打开一个文本文件

需要准备的材料分别是:电脑、linux连接工具。

1、首先连接上linux主机,进入到需要打开文件的目录,例如“/home/httpd/html/test”。

⑷ linux下读取文件夹中所有txt的名字存入新的txt中

system("ls /home/ionadmin/wrk/FileRead/*.txt > name.txt");

⑸ linux shell取得txt中字符串

$find . -type f -name "*.txt" | xargs grep version
结果:
./A/C/E/E567.txt:version 2.5
./A/C/C345.txt:version 2.4
./A/B/D/D456.txt:version 5.6
./A/A123.txt:version 3.4

如果不要上面的version,则可用下面的
$find . -type f -name "*.txt" | xargs grep version | awk '{print $1":"$2}'| awk -F \: '{print $1,$3}'
结果:
./A/C/E/E567.txt 2.5
./A/C/C345.txt 2.4
./A/B/D/D456.txt 5.6
./A/A123.txt 3.4

希望满足你的要求

⑹ linux读取文本文件out.txt

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的 只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。
如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

⑺ linux 怎么读取txt文件

1、连接上相应的linux主机,进入到等待输入shell指令的linux命令行状态下。

⑻ linux中怎么用命令打开文本文件

linux中怎么用命令打开文本文件的方法(利用Vim文本编辑器):

1、打开终端。点击菜单。

⑼ c语言如何读写 linux文本文件

Linux下C语言的文件(fputc,fgetc,fwrite,fread对文件读写操作)

//

fputc 向文件写入字符

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp;

char ch;

if((fp=fopen("test.txt","w"))==NULL)

{

printf("不能打开文件 ");

exit(0);

}

while ((ch=getchar())!=' ')

fputc( ch, fp );

fclose(fp);

}

-------------

小提示:

fp=fopen("test.txt","w") ,把"w"改为 "a" 可以创建文件并且追加写入内容

exit(0); 需要包含 stdlib.h 头文件,才能使用

//

fgetc 读取字符

#include <stdio.h>

#include <stdlib.h>

main( int argc, char *argv[] )

{

char ch;

FILE *fp;

int i;

if((fp=fopen(argv[1],"r"))==NULL)

{

printf("不能打开文件 ");

exit(0);

}

while ((ch=fgetc(fp))!=EOF)

putchar(ch);

fclose(fp);

}

文件结尾,通过判断 EOF

//

fwrite 的使用

使数组或结构体等类型可以进行一次性读写

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","wb"))==NULL)

{

printf("不能打开文件");

exit(0);

}

printf("请输入信息,姓名 年龄 分数1 分数2 地址: ");

for( i=0;i<2;i++)

{

scanf("%s %d %f %f %s",stu.name,&stu.age,&stu.score[0],&stu.score[1], stu.addr);

fwrite(&stu,sizeof(stu),1,fp1);

}

fclose(fp1);

}

//

fread 的使用

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp1;

int i;

struct student{

char name[10];

int age;

float score[2];

char addr[15];

}stu;

if((fp1=fopen("test.txt","rb"))==NULL)

{

printf("不能打开文件");

exit(0);

}

printf("读取文件的内容如下: ");

for (i=0;i<2;i++)

{

fread(&stu,sizeof(stu),1,fp1);

printf("%s %d %7.2f %7.2f %s ",stu.name,stu.age,stu.score[0],stu.score[1],stu.addr);

}

fclose(fp1);

}

//

fprintf , fscanf, putw , getw , rewind , fseek 函数

这些函数的话我就不演示了 ,

这些函数基本都一对来使用,例如 fputc 和 fgetc 一起来用.

⑽ Linux系统下怎样打开TXT文件呢

打开终端 进入文件所在目录
vi foo.txt

热点内容
跳转页源码 发布:2024-09-17 03:13:05 浏览:543
html文件上传表单 发布:2024-09-17 03:08:02 浏览:784
聊天软件编程 发布:2024-09-17 03:00:07 浏览:726
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207
cf编译后没有黑框跳出来 发布:2024-09-17 01:46:54 浏览:249
安卓怎么禁用应用读取列表 发布:2024-09-17 01:46:45 浏览:524
win10设密码在哪里 发布:2024-09-17 01:33:32 浏览:662
情逢敌手迅雷下载ftp 发布:2024-09-17 01:32:35 浏览:337
安卓如何让软件按照步骤自动运行 发布:2024-09-17 01:28:27 浏览:197