Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用fgets的C语言输出_C - Fatal编程技术网

使用fgets的C语言输出

使用fgets的C语言输出,c,C,下面是有问题的代码: FILE *fp; char str[256]; /* opening file for reading */ fp = fopen("file.txt" , "r"); if(fp == NULL) { perror("Error opening file"); return(-1); } while( fgets (str, sizeof(str), fp)) { int i

下面是有问题的代码:

FILE *fp;
    char str[256];
    /* opening file for reading */
    fp = fopen("file.txt" , "r");
    if(fp == NULL) {
        perror("Error opening file");
        return(-1);
    }
    while( fgets (str, sizeof(str), fp)) {
        int i;
        char *temp;
        temp=malloc(257);
        for(i=0;i<sizeof(str)-1;i++){
            if(isalpha(str[i])){
                append(temp,str[i]);
            }else{
                printf(" %s ",temp);
                temp=calloc(257,sizeof(char));
            }
        }
    }
然后它将输出以下内容:

"Here's a text
file example. No
idea what's wrong."
"Here s a text          vf       file example No               vf               idea what s wrong".
参考所需输出:

"Here s a text file example No idea what s wrong"

基本上每次有新线的时候都会有一些奇怪的东西。当我运行它时可能是“vf”。下次可能是“ZG”。每次我运行程序时它都会改变

读取未由fgets()填充的
buf
部分

替换

// for(i=0;i<sizeof(str)-1;i++)
for(i=0;i<strlen(str);i++)

//for(i=0;i也许我不明白您在做什么,但是如果您只想在一个字符串中读取整个文件,您可以这样做

int main(int argc, const char * argv[])
{
    FILE *fp;
    char str[256];
    char *temp = calloc(257, 1);
    fp = fopen("file.txt" , "r");
    if(fp == NULL) {
        perror("Error opening file");
        return(-1);
    }
    while( fgets (str, sizeof(str), fp)) {
        int i;
        //char *temp;

        if (str[strlen(str)-1] == '\n') {
            str[strlen(str)-1] = ' ';
        }
        strncat(temp, str, 257);
    }
    puts(temp);
}
fgets
没有问题。它会自动附加终止空字符

另外,在分配新内存之前,您也不会释放内存。这不好。

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

int main()
{
  FILE *fp;
  char str[256];
  /* opening file for reading */
  fp = fopen("file.txt" , "r");
  if(fp == NULL) {
    perror("Error opening file");
    return(-1);
  }
  while( fgets (str, sizeof(str), fp)) {
    int i;
    char temp[256]; //buffer; can allocate on the stack, no need for malloc
    char  *temp1;   //pointer at the beginning of the buffer (used to do my own append())
    temp1=temp;     //for my own appending
    for(i=0;i<sizeof(str);i++){
      int ch=str[i]; //alias the str[i] 
      if(isalpha(ch)||ch=='"'){ //let these chars thru
        *temp1++=ch; //my own append()
      }else if(ch=='\0'){//already at the end of buffer, end loop and print
        *temp1=ch; //don't forget to end the string with '\0' (for printing functions)
        break;
      }else if(ch=='.'){ // you seem to want to skip dots
        continue;
      }
      else {
        *temp1++=' '; //replace  other nonalpha characters with ' '
      }

    }
    printf("%s",temp);
  }
}
#包括 int main() { 文件*fp; char-str[256]; /*打开文件进行读取*/ fp=fopen(“file.txt”,“r”); 如果(fp==NULL){ perror(“打开文件时出错”); 返回(-1); } while(fgets(str,sizeof(str),fp)){ int i; char temp[256];//buffer;可以在堆栈上分配,无需malloc char*temp1;//缓冲区开头的指针(用于执行我自己的append()) temp1=temp;//用于我自己的附加
对于(i=0;i这里有一些我马上就能看到的东西:*您可能应该在每次使用之前将str缓冲区设置为零。*您可能应该在调用新的临时缓冲区之前释放临时缓冲区--您正在泄漏内存
#include <stdio.h>
#include <stdlib.h>

int main()
{
  FILE *fp;
  char str[256];
  /* opening file for reading */
  fp = fopen("file.txt" , "r");
  if(fp == NULL) {
    perror("Error opening file");
    return(-1);
  }
  while( fgets (str, sizeof(str), fp)) {
    int i;
    char temp[256]; //buffer; can allocate on the stack, no need for malloc
    char  *temp1;   //pointer at the beginning of the buffer (used to do my own append())
    temp1=temp;     //for my own appending
    for(i=0;i<sizeof(str);i++){
      int ch=str[i]; //alias the str[i] 
      if(isalpha(ch)||ch=='"'){ //let these chars thru
        *temp1++=ch; //my own append()
      }else if(ch=='\0'){//already at the end of buffer, end loop and print
        *temp1=ch; //don't forget to end the string with '\0' (for printing functions)
        break;
      }else if(ch=='.'){ // you seem to want to skip dots
        continue;
      }
      else {
        *temp1++=' '; //replace  other nonalpha characters with ' '
      }

    }
    printf("%s",temp);
  }
}