c - 当从单独的线程调用时,如何从 fgets 获得正确的输出?

标签 c multithreading fgets

我想在我创建的线程中逐行读取文件,但由于某种原因,我从 fgets 得到的结果只是垃圾。这个问题的原因是什么?我该如何解决它?

这是问题区域,它包含在其自己的线程中:

    FILE *orders = fopen("orders.txt","r");

    int numOrders;
    numOrders = getNumLines(orders);

    int orderLineSize = getMaxLineCount(orders);
    char Line[orderLineSize];


    fgets(Line,orderLineSize,orders); // This is the call that gives me a garbage output
    printf("Line is: %s\n",Line);

这是Line中包含的内容:;3+。但是,每次运行程序时都会发生变化

这就是 orders.txt 包含的内容:

"I Could Pee on This: And Other Poems by Cats"|7.49|1|HOUSING01
"Across Islands and Oceans"|1.99|1|SPORTS01
"Soul Surfer"|7.99|3|SPORTS01
"The Immortal Life of Henrietta Lacks"|8.91|4|POLITICS01
"The Handbuilt Home: 34 Simple Stylish and Budget-Friendly Woodworking Projects for    Every Room"|15.63|1|HOUSING01
"The History of Surfing"|31.5|2|SPORTS01

这是 getMaxLineCount:

int getMaxLineCount(FILE *filePtr)
{
    char c;
    int lineCount;
    int maxCount;
    lineCount = 0;
    maxCount = 0;
    while(c != EOF)
    {
            c = fgetc(filePtr);
            lineCount++;
            if((c == '\n') && (lineCount > maxCount))
            {
                    maxCount = lineCount;
                    lineCount = 0;
            }
    }

    rewind(filePtr);
    return maxCount;
}

获取行数:

int getNumLines(FILE *filePtr)
{
    char c;
    int lineCount;
    int maxCount;
    lineCount = 0;
    maxCount = 0;

    int peopleCount;
    peopleCount = 0;
    while(c != EOF)
    {
            c = fgetc(filePtr);
            if(c == '\n')
                    peopleCount++;
    }

    rewind(filePtr);
    return peopleCount;
}

最佳答案

我的猜测是 getMaxLineCount 正在消耗该文件,因此您必须倒带它。

在调用fgets之前,尝试添加

rewind(orders);

除非多个线程尝试读取同一文件,否则此问题应该与线程无关。

更新:以下简短的程序对我来说效果很好。

更新2:我至少修复了@chux 提到的一些未定义行为。

更新3:由chux更改为size_t。重新排序以允许测试最后一行是否最长。移动 lineCount = 0; (只有在找到新的最大值时才会发生。)

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


size_t getMaxLineCount(FILE *filePtr)
{
    int c; // Changed `char` to int
    size_t lineCount;
    size_t maxCount;
    lineCount = 0;
    maxCount = 0;
    while(1)
    {
        c = fgetc(filePtr);
        if (c == EOF) break;
        lineCount++;
        if (c == '\n')
        {
          if (lineCount > maxCount) maxCount = lineCount;
          lineCount = 0;
        }
    }
    if (lineCount > maxCount) maxCount = lineCount;

    rewind(filePtr);
    return maxCount;
}

int main(){
    FILE *orders = fopen("orders.txt","r");

    size_t orderLineSize = getMaxLineCount(orders);
    char Line[orderLineSize+1]; // Extra space for the buffer.


    fgets(Line,orderLineSize,orders); // This works fine for me.
    printf("Line is: %s\n",Line);
}

关于c - 当从单独的线程调用时,如何从 fgets 获得正确的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23139186/

相关文章:

c - 如何从系统函数调用中终止后台进程

Visual Studio 2012 中的 C

C - 读取 .txt 中的新行

c - 二叉树的层序遍历中出现运行时错误

c - 上证有效值计算

LLVM中的多线程

python time.sleep 花费的时间比预期的要长(多线程)

java - 将对象传递给另一个线程

c - While循环导致程序崩溃

C - 在函数中使用 fgets() 从标准输入读取行