㈠ python 把文件内容读到一个数组里
首先py的数组使用列表代替的,除非py的扩展工具包,比如py有一个支持矩阵的包里面有数组的概念,
将文件读到列表里:
f = open('file_name.txt','r')
f_list = f.readlines()
可以print(f_list)查看列表,这是将文件的所有内容一次性读到列表中
㈡ c语言 读取目录中的文件名,并将其存入数组中
用system 调用 DOS DIR 命令就可以了:
system ( "dir sss_* /B > log.txt");
这就把 前缀为sss_的文件 文件名 存入 log.txt 文件了。
一个名字一行,没有别的东西。
你再 读出来。
#include <stdio.h>
main()
{
FILE *fp;
char str[30][50]; // 假定文件数不超过30个
int i,n=0;
system("dir sss_* /B > log.txt");
fp=fopen("log.txt","r");
while(1){
if ( fgets(str[n],50,fp)==NULL) break;
str[n][strlen(str[n])-1]='\0'; // 加一个字符串结束符
n++;
}
fclose(fp);
for (i=0;i<n;i++) printf("%s\n",str[i]);
}
㈢ c++中怎么把读取文件夹下的所有文件名存入数组
1、在linux平台,可采用目录操作函数,读取当前目录下的文件
#include <sys/types.h>
#include <dirent.h> //windows开发工具没有这个头文件
#include <unistd.h>
#include <string.h>
main()
{
DIR * dir;
struct dirent * ptr;
char file_list[100][40];
int i=0;
dir = opendir("/etc/rc.d"); //打开一个目录
while((ptr = readdir(dir)) != NULL) //循环读取目录数据
{
printf("d_name : %s\n", ptr->d_name); //输出文件名
strcpy(file_list[i],ptr->d_name ); //存储到数组
if ( ++i>=100 ) break;
}
closedir(dir);//关闭目录指针
}
2、在windows平台下,如VC也有类似的操作函数,如:
#include <string>
#include <iostream>
using namespace std;
#include <WINDOWS.H>
void main()
{
string szPath="d:/*.*";
WIN32_FIND_DATA wfd;
HANDLE hFind;
char file_list[100][40];
int i=0;
hFind = FindFirstFile(szPath.c_str(), &wfd) ;
do
{
cout << wfd.cFileName << endl ;
strcpy(file_list[i],wfd.cFileName ); //存储到数组
if ( ++i>=100 ) break;
}while (FindNextFile(hFind, &wfd));
FindClose(hFind);
}
㈣ VB/Script 怎样读取一个目录下的所有文件名到数组
你可以使用opendir, closedir and readdir 这三种方法例如:
opendir DIR, $dir or die "cannot open dir $dir: $!";
my @file= readdir DIR;
closedir DIR;
㈤ C语言如何读取TXT文件并存入数组中
一、编程思路。
1 以文本方式打开文件。
2 循环用fscanf格式化输入数据到数组。
3 判断fscanf的返回值,如果显示到达文件结尾,退出输入。
4 关闭文件。
5 使用数据。
二、代码实现。
设定文件名为in.txt, 存有一系列整型数据,以空格或换行分隔。
代码可以写作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
int main()
{
int v[100];//开一个足够大的数组。
int i = 0, j;
FILE *fp;//文件指针
fp = fopen("in.txt", "r");//以文本方式打开文件。
if(fp == NULL) //打开文件出错。
return -1;
while(fscanf(fp, "%d", &v[i]) != EOF) //读取数据到数组,直到文件结尾(返回EOF)
i++;
fclose(fp);//关闭文件
for(j = 0; j < i; j ++)//循环输出数组元素。
{
printf("%d ", v[j]);
}
return 0;
}
当文件内容为:
1 35 6 8 9 9
10 123 34
76 54 98
程序输出:
1 35 6 8 9 9 10 123 34 76 54 98