- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有兴趣读取 .txt 文件并将其中的数据保存在 C 中的矩阵中。
dist.txt is the following:
Distance Amsterdam Antwerp Athens Barcelona Berlin
Amsterdam - 160 3082 1639 649
Antwerp 160 - 2766 1465 723
Athens 3082 2766 - 3312 2552
Barcelona 1639 1465 3312 - 1899
Berlin 649 723 2552 1899 -
其实它有更多的城市,但没关系。
我想阅读这份文件并记录距离。我尝试了以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define rows 6
#define cols 6
int main()
{
FILE *nansa;
char *buffer;
int ret,row=0,i,j;
char delims[]=" \t";
char *result=NULL;
double **mat=malloc( rows*sizeof(double*) );
for(i=0; i<rows; i++)
{
mat[i]=malloc( cols*sizeof(double) );
}
if ((nansa=fopen("dist.txt","r"))==NULL)
{
fprintf(stdout, "Error\n");
return -1;
}
while(!feof(nansa))
{
buffer=malloc( sizeof(char)*4096 );
memset(buffer,0,4096);
ret=fscanf(nansa, "%4095[^\n]\n", buffer);
if(ret != EOF)
{
int field=0;
result=strtok(buffer,delims);
while(result != NULL)
{
if(field>4) break;
mat[row][field]=atof(result);
result=strtok(NULL,delims);
field++;
}
++row;
}
free(buffer);
}
fclose(nansa);
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
printf("%g%s", mat[i][j], j<cols-1 ? "\t" : "\n");
free(mat[i]);
}
}
free(mat);
return 0;
}
但我没有得到我想要的...而且我不知道如何分隔名称和距离(字符和整数)。如果有人能帮助我,我将不胜感激!
最佳答案
虽然很想用fgets
来读取每一行(feof
是错误的),但问题只是一个有少量城市的例子:也许有10000。所以我假设任何一个城市的名字都小于64(仅供输入)。保留的内存对于名称的实际长度是正确的。
行和列将是相同的,所以定义不同没有意义:事实上我只定义了城市的数量。我为城市名称(与向下相同)和距离使用单独的数组。
为简单起见,我进行了错误检查,但在没有任何消息的情况下中止了。但是需要修改的地方是城市是一个多词名称,例如洛杉矶(%s
在任何空格处停止)。那么您将需要一种不同的方法,或者可能使用下划线来分隔 city_name。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define cities 5
int main(void){
FILE *nansa;
char buffer[64];
char distname[64]; // just to save work
char *city[cities]; // city names
int *dist[cities]; // distance array
int i, j, len, wid = 0;
if((nansa = fopen("dist.txt","r")) == NULL)
exit(1); // file open fault
// read the headings
if(fscanf(nansa, "%63s", buffer) != 1) // read the word for "distance"
exit(1); // fscanf fault
strcpy(distname, buffer);
for(i=0; i<cities; i++) { // read the city names
if(fscanf(nansa, "%63s", buffer) != 1)
exit(1); // fscanf fault
len = strlen(buffer) + 1;
if (wid < len)
wid = len; // column width
if((city[i] = malloc(len)) == NULL) // memory for city name
exit(1); // malloc fault
strcpy(city[i], buffer);
}
// read the data
for(j=0; j<cities; j++) { // read each table line
if((dist[j] = malloc(cities * sizeof(int))) == NULL) // memory for distance chart
exit(1); // malloc fault
if(fscanf(nansa, "%s", buffer) != 1) // skip the city name
exit(1); // fscanf fault
for(i=0; i<cities; i++) { // read each table line
if(fscanf(nansa, "%63s", buffer) != 1) // read the distance
exit(1); // fscanf fault
dist[j][i] = atoi(buffer);
}
}
fclose(nansa);
// display the table headings
printf("%-*s", wid, distname); // use the terminology in the file
for(i=0; i<cities; i++) // each city name
printf("%-*s", wid, city[i]);
printf("\n");
// display each line
for(j=0; j<cities; j++) {
printf("%-*s", wid, city[j]); // start with city name
for(i=0; i<cities; i++) { // each table data
if(dist[j][i])
printf("%-*d", wid, dist[j][i]);
else
printf("%-*c", wid, '-');
}
printf("\n");
}
// free the memory
for(i=0; i<cities; i++) {
free (city[i]);
free (dist[i]);
}
return 0;
}
程序输出:
Distance Amsterdam Antwerp Athens Barcelona Berlin
Amsterdam - 160 3082 1639 649
Antwerp 160 - 2766 1465 723
Athens 3082 2766 - 3312 2552
Barcelona 1639 1465 3312 - 1899
Berlin 649 723 2552 1899 -
关于c - 读取 .txt 文件并将数据保存为 C 中的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34531918/
我知道我要求太多,但也许你也可以帮助解决这个问题。 a.txt 包含单词,b.txt 包含字符串。 我想知道 b.txt 中有多少个字符串以 a.txt 中的单词结尾 例子:一个.txt apple
这个问题在这里已经有了答案: erge text files ordered by numerical filenames in Bash (3 个答案) 关闭 4 年前。 我有一个文件夹,其中包含
我在一个目录中有几个平面文件 (.txt)。所有这些文件的格式都是 *.txt.txt,所以我想将其重命名为 *.txt?有什么简单的方法可以一起重命名? 当我尝试 ren *.txt.txt *.t
这个问题在这里已经有了答案: How can I use a file in a command and redirect output to the same file without trunc
您是否有任何理由应该或不应该允许访问 javascript 或 css 文件?特别是常见的文件,如 jquery。 最佳答案 人们普遍认为,搜索引擎每天为给定站点分配一定数量的带宽或 URL。因此,一
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
这是相同的代码。我面临的问题是,我无法在任何文件上写入任何内容。请帮忙解决这个问题 #include #include int main() { FILE *fe; FILE *fo;
我想要特定于域的 robots.txt,到目前为止这有效: RewriteRule ^robots\.txt$ robots/%{HTTP_HOST}.txt [L] 但我希望有一个后备方案,因此如果
我正在调试一些构建成功运行的 SQL 命令的代码。 然而,在查询结束时,查询结果似乎被写入了一个文本文件。 完整的查询如下 echo SELECT DATE,DATETABLE,DATE,APPDAT
这个问题已经有答案了: difference between grep Vs cat and grep (5 个回答) 已关闭 8 年前。 我看到一个例子,其中有人这样做: cat source.tx
我想将表中的数据从以 csv 格式存储的文本文件插入到 sql server 表中。为此,我正在使用批量插入语句。现在我需要在“From”子句中指定文件名。我不想在那里使用网络位置或本地位置。我想将我
假设我有一个测试文件夹 (test.domain.com) 并且我不希望搜索引擎在其中抓取,我是否需要在测试文件夹中有一个 robots.txt 或者我可以只放置一个 robots.txt在根目录中,
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
这个问题在这里已经有了答案: order of directives in robots.txt, do they overwrite each other or complement each ot
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
已关闭。这个问题是 not about programming or software development 。目前不接受答案。 这个问题似乎不是关于 a specific programming
在过去的几年中,当我引用“名字”字段的文本框控件时,我一直使用 FirstNameTxt 命名约定。但是,我注意到大多数其他开发人员倾向于使用命名约定 txtFirstName 哪个是最好的约定?为什
我只想允许目录 /minsc 中的一个文件,但我想禁止该目录的其余部分。 现在 robots.txt 中是这样的: User-agent: * Crawl-delay: 10 # Directorie
我正在编写一个将 youtube.com 映射到另一个域的代理服务器(因此用户可以轻松地从德国等国家/地区访问 youtube,而无需审查搜索结果和视频)。 不幸的是,我的 robots.txt 中存
我没有编程技能,但有一项非常具体的任务:我必须将一个庞大的文本文件拆分成多个,并在特定的文本标记 (@) 处拆分它们。我决定尝试使用 Powershell 脚本来完成此任务。 到目前为止,这就是我想出
我是一名优秀的程序员,十分优秀!