- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我来这里是为了寻求帮助。我想读取以下格式的 .txt:
20
20
1 2
1 2
3 4 2 2
5 4 1 1 1 1
4 2 1 3 1
3 2 1 7
4 1 3 3 2
2 10 1
5 8 2 1 1 1
3 8 1 1
4 9 1 1 1
4 3 8 1 1
2 2 12
4 3 2 4 3
4 2 2 4 3
3 3 4 2
2 3 2
3 2 1 2
2 5 4
2 4 2
1 6
1 9
2 9 3
4 4 4 1 1
3 1 2 8
3 4 9 1
4 2 7 1 1
2 7 1
3 1 5 1
3 8 5 1
4 1 3 7 1
5 1 2 2 3 1
5 2 1 2 2 1
3 2 4 1
4 2 1 4 1
6 1 1 1 1 2 1
4 1 2 1 2
3 8 1 2
1 4
1 2
我写了一些代码。它复制 .txt 的每一行(我不想读取 20 行)并在运行时直接在 cmd 中打印。这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main( int argc, char *argv[]){
FILE*fp;
int i=0, dimension[2];
char row[128];
int n_rows, n_columns;
char *v_row, *v_col;
fp=fopen(argv[1],"r");
if(fp == NULL){
printf("File not found\n");
exit(0);
}
while(i<3){
fgets(row, 128, fp);
sscanf(row, "%d", &dimension[i]);
i++;
}
n_rows=dimension[0];
n_columns=dimension[1];
printf("dimension: %d x %d\n\n", n_rows, n_columns);
v_row=malloc((n_rows+1)*sizeof(char));
v_col=malloc((n_columns+1)*sizeof(char));
for(i=0; i<n_rows; i++){
fgets(row, 128, fp);
printf("row: %s\n", row);
}
for(i=0;i<n_columns; i++){
fgets(row, 128, fp);
printf("row: %s\n", row);
sscanf(row,"%s", &v_col[i]);
}
fclose(fp);
exit(-1);
}
但这不是我的最终目标。我真正想做的是将每一行放入一个数组中,将第一个数字拆分为一个数组,并将该行的其余部分拆分为另一个数组,但不幸的是我无法找到一种方法来做到这一点。
最佳答案
其实你自己已经得到了答案。使用 sscanf() 完成您的工作
示例::
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *tmp = "123 456 789 123 4652 128793";
char arr[5], arr2[30];
printf("tmp::\t%s\n",tmp);
sscanf(tmp,"%s %[^\n]s",arr,arr2);
printf("arr::\t%s\n",arr);
printf("arr2::\t%s\n",arr2);
return 0;
}
输出:
tmp:: 123 456 789 123 4652 128793
arr:: 123
arr2:: 456 789 123 4652 128793
编辑::
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 42 // max length of line
int main()
{
FILE *fp; // for the file
int Nrow, Ncol;
int loop;
char line[LENGTH];
char **arrRows_1stDim, **arrRows_2ndDim;
char **arrCols_1stDim, **arrCols_2ndDim;
char arr[20], arr2[20];
fp = fopen("testDATA.txt","r"); // I saved your file with this name
if(fp == NULL)
{
perror("testDATA.txt");
return -1;
}
fscanf(fp,"%d", &Nrow);
fscanf(fp,"%d", &Ncol);
printf("Nrow::%d,\tNcol::%d\n",Nrow,Ncol);
arrRows_1stDim = malloc(sizeof(char*) * Nrow);
arrRows_2ndDim = malloc(sizeof(char*) * Nrow);
arrCols_1stDim = malloc(sizeof(char*) * Ncol);
arrCols_2ndDim = malloc(sizeof(char*) * Ncol);
// check if our malloc() was allocated
if(arrRows_1stDim == NULL || arrRows_2ndDim == NULL || arrCols_1stDim == NULL || arrCols_2ndDim == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
printf("Reading Rows...\n\n");
for(loop=0;loop<Nrow;loop++)
{
fgets(line,LENGTH,fp);
if(strlen(line)==1)
loop--;
else
{
sscanf(line,"%s %[^\n]s",arr,arr2);
//printf("Arr::%s,\tArr2::%s\n",arr,arr2);
arrRows_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1));
arrRows_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1));
// check if our malloc() was allocated
if(arrRows_1stDim[loop] == NULL || arrRows_2ndDim[loop] == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
strcpy(arrRows_1stDim[loop],arr);
strcpy(arrRows_2ndDim[loop],arr2);
//printf("arrRows_1stDim[loop]::%s,\tarrRows_2ndDim[loop]::%s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]);
}
}
printf("\nReading Cols...\n\n");
for(loop=0;loop<Ncol;loop++)
{
fgets(line,LENGTH,fp);
if(strlen(line)==1)
loop--;
else
{
sscanf(line,"%s %[^\n]s",arr,arr2);
//printf("Arr::%s,\tArr2::%s\n",arr,arr2);
arrCols_1stDim[loop] = malloc(sizeof(char) * (strlen(arr) + 1));
arrCols_2ndDim[loop] = malloc(sizeof(char) * (strlen(arr2) + 1));
// check if our malloc() was allocated
if(arrCols_1stDim[loop] == NULL || arrCols_2ndDim[loop] == NULL)
{
fprintf(stderr,"Couldn't malloc()");
return -1;
}
strcpy(arrCols_1stDim[loop],arr);
strcpy(arrCols_2ndDim[loop],arr2);
}
}
printf("Let's see whether we read correctly or not...\n");
printf("Printing Rows...\n");
for(loop=0;loop<Nrow;loop++)
{
printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrRows_1stDim[loop],arrRows_2ndDim[loop]);
}
printf("\nPrinting Cols...\n");
for(loop=0;loop<Ncol;loop++)
{
printf("Rows_1stArray:: %s,\t Rows_2ndArray:: %s\n",arrCols_1stDim[loop],arrCols_2ndDim[loop]);
}
printf("\nFree Allocated Memory...\n");
// free inner level
for(loop=0;loop<Nrow;loop++)
{
free(arrRows_1stDim[loop]);
free(arrRows_2ndDim[loop]);
}
// free outer level
free(arrRows_1stDim);
free(arrRows_2ndDim);
for(loop=0;loop<Ncol;loop++)
{
free(arrCols_1stDim[loop]);
free(arrCols_2ndDim[loop]);
}
free(arrCols_1stDim);
free(arrCols_2ndDim);
fclose(fp);
printf("Done!\n");
return 0;
}
输出::
Nrow::20, Ncol::20
Reading Rows...
Reading Cols...
Let's see whether we read correctly or not...
Printing Rows...
Rows_1stArray:: 1, Rows_2ndArray:: 2
Rows_1stArray:: 1, Rows_2ndArray:: 2
Rows_1stArray:: 3, Rows_2ndArray:: 4 2 2
Rows_1stArray:: 5, Rows_2ndArray:: 4 1 1 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 1 3 1
Rows_1stArray:: 3, Rows_2ndArray:: 2 1 7
Rows_1stArray:: 4, Rows_2ndArray:: 1 3 3 2
Rows_1stArray:: 2, Rows_2ndArray:: 10 1
Rows_1stArray:: 5, Rows_2ndArray:: 8 2 1 1 1
Rows_1stArray:: 3, Rows_2ndArray:: 8 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 9 1 1 1
Rows_1stArray:: 4, Rows_2ndArray:: 3 8 1 1
Rows_1stArray:: 2, Rows_2ndArray:: 2 12
Rows_1stArray:: 4, Rows_2ndArray:: 3 2 4 3
Rows_1stArray:: 4, Rows_2ndArray:: 2 2 4 3
Rows_1stArray:: 3, Rows_2ndArray:: 3 4 2
Rows_1stArray:: 2, Rows_2ndArray:: 3 2
Rows_1stArray:: 3, Rows_2ndArray:: 2 1 2
Rows_1stArray:: 2, Rows_2ndArray:: 5 4
Rows_1stArray:: 2, Rows_2ndArray:: 4 2
Printing Cols...
Rows_1stArray:: 1, Rows_2ndArray:: 6
Rows_1stArray:: 1, Rows_2ndArray:: 9
Rows_1stArray:: 2, Rows_2ndArray:: 9 3
Rows_1stArray:: 4, Rows_2ndArray:: 4 4 1 1
Rows_1stArray:: 3, Rows_2ndArray:: 1 2 8
Rows_1stArray:: 3, Rows_2ndArray:: 4 9 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 7 1 1
Rows_1stArray:: 2, Rows_2ndArray:: 7 1
Rows_1stArray:: 3, Rows_2ndArray:: 1 5 1
Rows_1stArray:: 3, Rows_2ndArray:: 8 5 1
Rows_1stArray:: 4, Rows_2ndArray:: 1 3 7 1
Rows_1stArray:: 5, Rows_2ndArray:: 1 2 2 3 1
Rows_1stArray:: 5, Rows_2ndArray:: 2 1 2 2 1
Rows_1stArray:: 3, Rows_2ndArray:: 2 4 1
Rows_1stArray:: 4, Rows_2ndArray:: 2 1 4 1
Rows_1stArray:: 6, Rows_2ndArray:: 1 1 1 1 2 1
Rows_1stArray:: 4, Rows_2ndArray:: 1 2 1 2
Rows_1stArray:: 3, Rows_2ndArray:: 8 1 2
Rows_1stArray:: 1, Rows_2ndArray:: 4
Rows_1stArray:: 1, Rows_2ndArray:: 2
Free Allocated Memory...
Done!
关于c - 将文件 .txt 读取到数组并拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285200/
我知道我要求太多,但也许你也可以帮助解决这个问题。 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 脚本来完成此任务。 到目前为止,这就是我想出
我是一名优秀的程序员,十分优秀!