【问题标题】:Taking structure's inputs from a file and print it in C [closed]从文件中获取结构的输入并在 C 中打印 [关闭]
【发布时间】:2012-12-24 16:48:12
【问题描述】:

当我尝试打印expectedRuntime 变量时,它会打印expectedRuntime 的地址。 但我可以正确打印 timeOfSubmission 变量。谁能帮帮我?

struct process
{
int timeOfSubmission;
int remainingRunTime;
int expectedRunTime;
char processName[20];

};

int main()
{
FILE *myInput;
myInput = fopen("input.txt", "r+");
while ( !feof(myInput) )
{

    struct process * newProcess=(struct process *)malloc(sizeof(struct process));

    fscanf(myInput, "%s", newProcess->processName);
    fscanf(myInput, "%d", & (newProcess->expectedRunTime) );
    (newProcess->expectedRunTime)=(newProcess->remainingRunTime);
    fscanf(myInput, "%d", & (newProcess->timeOfSubmission) );
    printf("%s    ",newProcess->processName);
    fflush(stdout);
    printf("%d    ",newProcess->expectedRunTime);
    fflush(stdout);
    printf("%d  \n",newProcess->timeOfSubmission);
    fflush(stdout); 
}
return 0;
}

【问题讨论】:

    标签: c file structure printf scanf


    【解决方案1】:

    看完newProcess->expectedRunTime, 您正在用以下内容覆盖它:

    fscanf(myInput, "%d", & (newProcess->expectedRunTime) );
    (newProcess->expectedRunTime)=(newProcess->remainingRunTime);
    

    其中有垃圾,因为没有任何内容写入newProcess->remainingRunTime

    【讨论】:

    • 我换了一边,比如:(newProcess->remainingRunTime)=(newProcess->expectedRunTime);它确实有效。到目前为止,这个小东西毁了我的整个项目:)非常感谢!跨度>
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2020-12-19
    • 2012-04-23
    • 2021-11-17
    相关资源
    最近更新 更多