【问题标题】:Not printing first 5 characters of the file不打印文件的前 5 个字符
【发布时间】:2014-06-17 01:16:38
【问题描述】:

从文件读取并打印结果的循环不会打印所有读取文件的前 5 个字符。如果我一次打印它们 1 个字符,它工作正常,但我正在从需要稍后处理的文件中读取浮点数。这些数字由空格分隔,这就是为什么我只在遇到空格时才尝试打印。

这是我的源代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>

#define BUFFER_SIZE 100
#define READ_END 0
#define WRITE_END 1

//main function
int main(int argc, char *argv[])
{
    //process ids
    int processIds[argc - 1];
    int pipes[argc - 1][2];
    char *fileNames[argc - 1];
    int status; 

    //check if at least one data file
    if (argc == 1)
    {
        return -1;
        //end program    
    }

    //get file names and store in array fileNames
    int i;
    for (i = 0; i < (argc - 1); i++)
    {
        fileNames[i] = argv[i + 1];
    }    

    //create child processes
    for (i = 0; i < (argc - 1); i++)
    {
        //make a pipe for communication
        if (pipe(pipes[i]))
        {
            printf("bad pipe");
            return -1;
        }
        //make a child process
        processIds[i] = fork();

        //check if child or paren process
        if (processIds[i] == 0)
        {
            //child process
            //close unused end

            close(pipes[i][READ_END]);

            //make file
            FILE *dataset = fopen(fileNames[i], "r");

            //test if file opened
            if (dataset == 0)
            {
                printf("could not find/open file");
                return -1;
            }

            //read and process file
            char *x;
            char *num = "";
            int min, max;
            while ((*x = fgetc(dataset)) != EOF)
            {
                if ((x == " ") || (x == "\t") || (x == "\n"))
                {
                    //end of number
                    printf("A%s B%s", num, num);
                    num = "";
                }
                else
                {
                    strcat(num, x);
                }
                //printf("%c", x);    
            }
            printf("\n\n");
            char msg[BUFFER_SIZE];

            write(pipes[i][WRITE_END], msg, strlen(msg) + 1);
            fclose(dataset);
            exit(0);
        }
        else if (processIds[i] < 0)
        {
            //error
            return -1;    
        }
        else
        {
            //parent process closes write end of pipe
            close(pipes[i][WRITE_END]);
        }
    }

    //wait for children
    for (i = 0; i < (argc - 1); i++)
    {
        //create a read buffer
        char read_buf[BUFFER_SIZE];

        //wait and get pid of completed child
        int pid = wait(&status);

        //find which child completed
        int j = 0;
        while (pid != processIds[j] && j < (argc - 1))
        {
            //pid not recognized, should not happen
            if (j >= (argc - 1))
            {
                printf("bad pid");
                return -1;
            }
            j++;
        }
        //read from completed child
        read(pipes[j][READ_END], read_buf, BUFFER_SIZE);
    }

    return 0;
}

【问题讨论】:

  • 您没有为num 分配任何内存,因此使用strcat 附加到它是未定义的行为。 msg 也被单元化了。
  • 我知道 SO 不能将制表符作为缩进字符正确处理是一件令人头疼的事,但是当您发布代码时,您确实需要努力正确缩进,这是唯一的方法即用 4 个空格替换所有制表符。有了一个体面的编辑器,这应该只是几秒钟的工作。我只是为你做的,但花费的时间比它的价值要长。

标签: c printf fgetc


【解决方案1】:

我可以发现以下关于您的代码的遗漏:

1) 内存分配

你需要分配你的num变量

示例:

char *num = malloc(10);

2) 返回类型

'fgetc' 既不返回指针也不返回字符,所以你应该定义

int x;

x = fgetc(dataset);

3) 你的条件不对

if ((x == ' ') || (x == '\t') || (x == '\n'))

注意' ' 不是" "


NOW 作为我阅读这些单独字符串的建议:

1) 读取缓冲区中的所有文件:

char buff[SIZE];
dataset= fopen("fileName.txt", "r");
   if( dataset!= NULL ){
      while ( !feof(fp ) ){
         memset(buff, '\0', sizeof( buff) );
         fgets(buff, SIZE, (FILE*)dataset);
      }
      fclose(dataset);
   }

2) 使用strchr 查找下一个space 并打印您的字符串 see example of use here

【讨论】:

  • 绝对是@MattMcNabb 它返回int
  • +1 但轻微的抱怨:您将buff 分配给SIZE,然后在fgets 调用中使用255。使用sizeofSIZE
猜你喜欢
  • 1970-01-01
  • 2020-06-28
  • 2012-08-05
  • 2021-12-09
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 2022-12-24
  • 2018-11-19
相关资源
最近更新 更多