【发布时间】:2018-03-25 00:48:52
【问题描述】:
大家好,感谢您的点击!(讨厌被卡住)
我正在尝试将一个文件中的 char 值 fscanf 到我的结构中的一个变量。 当我扫描时,我收到一封与我想要得到的完全不同的信。问题出在我的 readfile 函数中。如果我解决了这个问题,我希望我可以扫描我需要进行算术运算的数字。我的教授正在教我们 FCFS(与 OS 调度算法有关,而不是 FCFS 数据结构队列课程)。所以文本文件列的意思是(PAS.txt):
进程名称 |到达时间 |服务时间
A 0 3
B 2 6
C 4 4
D 6 5
E 8 2
//Main.c
#include <stdio.h>
#include <time.h>
#include "lab8Func.h"
int main(int argc, const char * argv[]) {
struct FCFS process;
readFile(&process);
printf("%s",&process.jobList[0].processName)
}
//lab8func.h
#ifndef lab8Func_h
#define lab8Func_h
struct Job
{
char processName;
int arrivalTime;
int serviceTime;
int TAT;
int NTAT;
};
struct FCFS
{
struct Job jobList[5];
};
void readFile(struct FCFS*);
#endif /* lab8Func_h */
#include <stdio.h>
#include "lab8Func.h"
void readFile(struct FCFS *process1)
{
FILE *file;
char temp;
int tempAT;
int tempST;
if((file = fopen("/Users/Vin/desktop/PAS.txt","r")) == NULL)
printf("Error, File Not Open.");
else
{
for(int i=0 ; i < 1; i++)
{
temp = fscanf(file," %c", &temp); // where I'm stuck..
process1->jobList[i].processName = temp;
}
}
}
输出
bProgram ended with exit code: 0
***小写b ??如何 ?我在找大写A!!*****
【问题讨论】:
-
1)
fscanf返回成功读取的元素数。temp = fscanf(file," %c", &temp);-->fscanf(file, " %c", &temp); -
2)
printf("%s",&process.jobList[0].processName)-->printf("%c", process.jobList[0].processName) -
函数:
main()有两个有效签名。这些签名是:int main( int argc, char *argv[] )和int main( void )。如果不打算使用传递的参数,则使用签名:int main( void )。编译时,始终启用警告,然后修复这些警告。 (对于gcc,至少使用:`-Wall -Wextra -pedantic -Wconversion -std=gnu11) -
函数中:
readFile(),局部变量tempAt和tempSt没有被使用,所以应该去掉。 -
在调用 fopen() 和 fscanf() 等系统函数时,始终检查返回值以确保操作成功。注意:
fscanf()返回一个整数,其中包含成功输入的成功输入/格式说明符的数量或 EOF。它不会返回char。&temp参数获取从(在这种情况下)file读取的值。将函数的返回值分配给temp只是用一些 1 字节的整数值覆盖temp。建议:if( 1 != fscanf(file," %c", &temp) ) { // handle error and exit }