我有一个名为test.txt的文件。我想从文件中逐个字符地阅读。然后开始从“开始”写入“停止”新文件,其名称为main.txt。我尝试编码,但它没有运行。请帮帮我。
#include<stdio.h>
//#include<conio.h>
FILE *fpR, *fpW;
char RFile[25],WFile[25],stArt[5],stOp[4],swA[5],swO[4];
char *c;
int cc=0,i=0;
// clrscr();
//Readin file's open process
printf("Please! Enter the name of the file to be read : \n");
scanf("%s",RFile);
//Writing file's open process
printf("Please! Enter the name of the file to be write : \n");
scanf("%s",WFile);
//Openin files
fpR = fopen(RFile,"r");
if (fpR==NULL) {
printf("Could not open %s!\n",RFile);
return 1;
}
fpW = fopen(WFile,"w");
if (fpW==NULL) {
printf("Could not open %s!\n",WFile);
return 1;
}
do {
for(i = 1;i <= 5;i++) {
swA[i] = fgetc(fpR);
if (swA=="start"){
fprintf(fpW,"%s",swA);
fprintf(stdout,"%s",swA);
}
for(i = 1;i <= 4;i++) {
swO[i] = fgetc(fpR);
if (swO=="stop"){
break;
}
}while (c != EOF);
// Close files
fclose(fpR);
fclose(fpW);
// getch();
return 0;
}
testfileisitozetoPıorkgldstartfldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9stopCCCINCISIweklemfkfKER
fldsfslf
1lsfslHkf12e43Y54465kds2cmSb3cmb4 op3I3533
5cmkr3rCdqe22e43S5446T5ztop5U6l271Rlr2l83KlccSck49
kr3rdWqe2I2e4354N465Sop33E533tC
VtteEe5R56l271Tlr2l83IlcMcSck4E9
答案 0 :(得分:2)
您应该使用strcmp()代替==
来检查两个字符串是否相等。
即使您正在针对c
进行检查,您似乎也不会使用变量EOF
。您确定c
应该是char *
吗?
更准确地描述您遇到的错误,并以较小的步骤编写代码,以便更容易找到您的错误。
答案 1 :(得分:1)
你可以在这里使用fgets()将整个文件存储到一个数组中。
在你的代码中 -
do {
for(i = 1;i <= 5;i++)
{
swA[i] = fgetc(fpR);
if (swA=="start"){
fprintf(fpW,"%s",swA);
fprintf(stdout,"%s",swA);
}
for(i = 1;i <= 4;i++)
{
swO[i] = fgetc(fpR);
if (swO=="stop"){
break;
}
}while (c != EOF);
而不是这个你可以使用fgets() -
#define MAX_LEN 1024
char ch[MAX_LEN];
while(fgets(ch,MAX_LEN,fpR))
{
fprintf(fpW,"%s",ch);
}
您不必检查“停止”或EOF,因为fgets()本身会在遇到EOF时返回。
答案 2 :(得分:0)
我使用strcmp()
命令修改了代码,删除了*
而不是使用cygwin64编译代码。
$ gcc --version
gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -o mread mread.c
$ ./mread
Please! Enter the name of the file to be read :
test.txt
Please! Enter the name of the file to be write :
main.txt
Segmentation fault (core dumped)
#include<stdio.h>
//#include<conio.h>
main()
{
FILE *fpR, *fpW;
char RFile[25],WFile[25],stArt[5]={"start"},stOp[4]={"stop"},swA[5],swO[4];
char c;
int cc=0,i=0;
// clrscr();
//Readin file's open process
printf("Please! Enter the name of the file to be read : \n");
scanf("%s",RFile);
//Writing file's open process
printf("Please! Enter the name of the file to be write : \n");
scanf("%s",WFile);
//Openin files
fpR = fopen(RFile,"r");
if (fpR==NULL) {
printf("Could not open %s!\n",RFile);
return 1;
}
fpW = fopen(WFile,"w");
if (fpW==NULL) {
printf("Could not open %s!\n",WFile);
return 1;
}
do {
c = fgetc(fpR);
fprintf(stdout,"%s",c);
for(i = 1;i <= 5;i++) {
c = fgetc(fpR);
swA[i]= c;
if (stArt==swA){
fprintf(fpW,"%s",swA);
fprintf(stdout,"%s",swA);
}
}
for(cc = 1;cc <= 4;cc++) {
swO[cc] =fgetc(fpR);
if (stArt==swO){
break;
}
}
} while (c != EOF);
// Close files
fclose(fpR);
fclose(fpW);
// getch();
return 0;
}