C 在文本文件中搜索由空格分隔的字符串,并在空格后删除所有内容

C 在文本文件中搜索由空格分隔的字符串,并在空格后删除所有内容,c,string,C,String,我想在字符串被每行上的空格分隔后,切断文本文件中的所有内容,例如,如果文本文件包含该行: I am a String. Iam a String too. 在输出文件的第一行上,输出文件中只有I,在第二行上,该行左侧只有Iam #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *input,*outpu

我想在字符串被每行上的空格分隔后,切断文本文件中的所有内容,例如,如果文本文件包含该行:

I am a String.
Iam a String too.
在输出文件的第一行上,输出文件中只有
I
,在第二行上,该行左侧只有
Iam

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *input,*output;
    char line[40];
    if(argc == 3){
        input = fopen(argv[1],"r");
        output = fopen(argv[2],"w");
        if(input!= NULL){
                while(!feof(input)){
                    fgets(line,sizeof line,input);
                    strtok(line," ");                    
                    fputs(line,output);                  

                }
                fclose(input);
                fclose(output);
        }
    }
}

#包括
#包括
#包括
int main(int argc,char*argv[])
{
文件*输入,*输出;
字符行[40];
如果(argc==3){
输入=fopen(argv[1],“r”);
输出=fopen(argv[2],“w”);
如果(输入!=NULL){
而(!feof(输入)){
fgets(行、行大小、输入);
斯特托克(行“”);
FPUT(线路、输出);
}
fclose(输入);
fclose(输出);
}
}
}

这就是它几乎完全是这样工作的,尽管它使更多的行相互混淆://

你想要这样的东西:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char* argv[])
{
  if (argc != 3)
  {
    display error message
    return 1;
  }

  FILE *infile = fopen(argv[1], "r");  // Use "r", not 'w'.
                                       // Use consistant variable naming: infile and outfile
  if (infile == NULL)                  // file could be opened?
  {
    display error message
    return 1;
  }

  FILE *outfile = fopen(argv[2], "w");   // Use "w", not 'a'
  if (outfile == NULL)                   // file could be opened?
  {
    display error message

    fclose(infile);                   // close infile that is obviously open here
    return 1;
  }

  do
  {
    char line[1000];

    if (fgets(line, sizeof line, infile) == NULL)   // read complete line
      break;                                        // enf of file : stop loop

      process line

      fputs(line, outfile);                         // write line
  } while (1);

  fclose(infile);
  fclose(outfile);
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
如果(argc!=3)
{
显示错误消息
返回1;
}
FILE*infle=fopen(argv[1],“r”);//使用“r”,而不是“w”。
//使用一致变量命名:infile和outfile
如果(infle==NULL)//文件可以打开吗?
{
显示错误消息
返回1;
}
FILE*outfile=fopen(argv[2],“w”);//使用“w”,而不是“a”
if(outfile==NULL)//是否可以打开文件?
{
显示错误消息
fclose(infle);//关闭此处明显打开的infle
返回1;
}
做
{
字符行[1000];
if(fgets(line,sizeof line,infle)==NULL)//读取完整行
break;//文件的enf:stop循环
生产线
fputs(行,输出文件);//写入行
}而(1),;
fclose(infle);
fclose(输出文件);
}
仍然有代码可供您编写。
此代码中可能有错误,但您应该了解其中的含义。

您应该区分“字符”和“字符串”。(C中的字符串是NUL终止的字符数组)。我想你只是指“空格”,而不是“回退”。你应该考虑限制嵌套,因为这使得代码更难读、理解、跟踪和维护。也请阅读你想要的算法:1。读一行。2.处理行,即在第一次出现
'
(空格)时放入
NUL
字符。3.写一行。重复此操作直到文件结束。提示3:阅读初学者C课本中关于字符串的章节。是的,我看到了在答案中加入代码是很难看的,而切换代码是不起作用的。非挥发性物质。strtok使用“”作为分隔符的解决方案正在将其截断,但会重复这样的代码:I am a String Iam a String Im a String>>>IaaString出于任何原因,尽管我阅读了多次,但我必须使用它:/