如何在文件中复制 m 行的 n 个字符(在 c 中)?
How do i copy n characters for m lines in a file (in c)?
我有一个 1000 行的文件,其中最多 70 个字符。我想先在一个 table 中复制第 13 个,在另一个 table 中复制第 15-30 个...我尝试了很多不同的方法但没有成功,这是其中之一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size_num 13 // 13 character with [=10=] included
#define line 1000
#define carac 300 // like 300 characters per line
int verifFile(FILE*pf) //check file
{
if (pf == NULL)
{
printf("can't open file");
return 1;
}
else
{
return 0;
}
}
int main()
{
float td,tf;
td=clock(); //count execution time of program
FILE* pf=NULL;
pf = fopen("call.txt", "r");
verifFile(pf);
char str[line];
char number[line][size_num];
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
//of each 1000 lines
}
printf("\nsecond : \n"); // separates the printings
for(int i;i<1001;i++)
{
for(int j;j<14;j++)
{
printf("%c",number[i][j]); //supposed to print each charac stored (doesnt work)
}
}
tf=clock();
printf("\n execution time : %f secondes",(tf-td)/CLOCKS_PER_SEC); //display execution time
return 0;
}
没有警告,所以我不知道该怎么办:/
您复制子字符串的方式有问题:
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
//of each 1000 lines
}
首先,你总是分配给 number[1000][13]
这是非法的,因为 number
只有 1000 行,即最大索引是 999
。此外,每行只有 13 个元素,使 12 成为最大索引。
相同的越界访问发生在 str
.
那么您没有任何计数器指示您要在数组的哪个元素中复制字符串。
最后,不能通过简单的赋值来复制字符串。您需要使用 strcpy
或 strncpy
.
这个循环应该是这样的:
int row = 0;
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
strncpy(number[row], &str[0], size_num);
number[row][size_num-1] = 0; // Don't forget to terminate the string!
// Add copying the other parts here as well...
row++;
}
那你打印内容也有问题:
for(int i;i<1001;i++)
{
for(int j;j<14;j++)
{
printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
}
}
同样,您有越界访问权限。
即使文件只包含几行,您也会打印所有可能的行。
此外,您甚至没有将循环计数器初始化为 0。
改为这样做:
for(int i = 0; i<row;i++)
{
#if 0
// use this part if you really want to print char by char...
for(int j = 0; j<size_num; j++)
{
printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
}
#else
// Use this if you want to print the string at once.
printf("%s\n",numero[i]);
#endif
}
我有一个 1000 行的文件,其中最多 70 个字符。我想先在一个 table 中复制第 13 个,在另一个 table 中复制第 15-30 个...我尝试了很多不同的方法但没有成功,这是其中之一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size_num 13 // 13 character with [=10=] included
#define line 1000
#define carac 300 // like 300 characters per line
int verifFile(FILE*pf) //check file
{
if (pf == NULL)
{
printf("can't open file");
return 1;
}
else
{
return 0;
}
}
int main()
{
float td,tf;
td=clock(); //count execution time of program
FILE* pf=NULL;
pf = fopen("call.txt", "r");
verifFile(pf);
char str[line];
char number[line][size_num];
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
//of each 1000 lines
}
printf("\nsecond : \n"); // separates the printings
for(int i;i<1001;i++)
{
for(int j;j<14;j++)
{
printf("%c",number[i][j]); //supposed to print each charac stored (doesnt work)
}
}
tf=clock();
printf("\n execution time : %f secondes",(tf-td)/CLOCKS_PER_SEC); //display execution time
return 0;
}
没有警告,所以我不知道该怎么办:/
您复制子字符串的方式有问题:
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
number[line][size_num]=str[line]; // here i want number to copy the 13 first characters
//of each 1000 lines
}
首先,你总是分配给 number[1000][13]
这是非法的,因为 number
只有 1000 行,即最大索引是 999
。此外,每行只有 13 个元素,使 12 成为最大索引。
相同的越界访问发生在 str
.
那么您没有任何计数器指示您要在数组的哪个元素中复制字符串。
最后,不能通过简单的赋值来复制字符串。您需要使用 strcpy
或 strncpy
.
这个循环应该是这样的:
int row = 0;
while(fgets(str,line,pf)!= NULL)
{
printf("%s", str);
strncpy(number[row], &str[0], size_num);
number[row][size_num-1] = 0; // Don't forget to terminate the string!
// Add copying the other parts here as well...
row++;
}
那你打印内容也有问题:
for(int i;i<1001;i++)
{
for(int j;j<14;j++)
{
printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
}
}
同样,您有越界访问权限。 即使文件只包含几行,您也会打印所有可能的行。 此外,您甚至没有将循环计数器初始化为 0。
改为这样做:
for(int i = 0; i<row;i++)
{
#if 0
// use this part if you really want to print char by char...
for(int j = 0; j<size_num; j++)
{
printf("%c",numero[i][j]); //supposed to print each charac stored (doesnt work)
}
#else
// Use this if you want to print the string at once.
printf("%s\n",numero[i]);
#endif
}