繁体   English   中英

***使用fscanf从C中的文件读取,扫描并打印.txt文件中不存在的字符

[英]***Reading from file in C*** using fscanf, Scanning and printing a char that doesn't exist in .txt file

大家好,谢谢点击!(讨厌卡住了)

我试图将一个文件的char值fscanf转换为struct中的变量。 扫描时,我收到的信件与我尝试收到的信件完全不同。 问题出在我的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;


        }


    }

}

OUTPUT

bProgram ended with exit code: 0 

***小写b ?? 怎么样 ? 我正在寻找大写字母A!*****

首先,fscanf的返回值是成功写入的参数数量(或失败时返回EOF)。 因此,您将覆盖temp变量。

temp = fscanf(file," %c", &temp);

其次,此打印语句是错误的:

printf("%s",&process.jobList[0].processName)

%s表示打印以null结尾的字符串,并且您正在将其传递给char的指针。 它们都属于char *类型,这就是为什么要编译的原因,但是它们不能像预期的那样起作用,并且可能崩溃。

printf("%c",process.jobList[0].processName)

第三,您忘记使用fscanf读取循环内的所有三列。

建议的代码如下:

  1. 干净地编译
  2. 自我清除后(在这种情况下,关闭输入文件)
  3. 检查并处理错误
  4. 将错误消息输出到stderr而不是stdout
  5. 遇到问题时退出
  6. 不包含“魔术”数字。 IE硬编码为“ 5”
  7. 从输入文件最多读取5行,而不是一行
  8. 更正已发布代码中的(几个)语法错误
  9. 执行所需的功能
  10. 仍然使用fscanf()尽管更好的调用是fgets() ,因为它将消除包含getc()的代码块
  11. 消除不必要的局部变量
  12. main()函数使用正确的签名
  13. 说明为什么包含每个头文件
  14. 遵循公理: 每行仅一个语句,每个语句(最多)一个变量声明。
  15. 将变量声明保留在使用它的地方。
  16. 尽力改善传递给函数的变量的“含义”: readFile()

警告:为了方便起见,我将代码布局修改为一个文件

现在建议的代码:

#include <stdio.h>   // fopen(), fclose(), fscanf(), perror()
#include <stdlib.h>  // exit(), EXIT_FAILURE
//#include <time.h>
//#include "lab8Func.h"

//lab8func.h
#ifndef lab8Func_h
#define lab8Func_h

#define MAX_RECORDS 5

struct Job
{
    char processName;
    int  arrivalTime;
    int  serviceTime;
    int  TAT;
    int  NTAT;

};

struct FCFS
{
    struct Job jobList[ MAX_RECORDS ];
};

void readFile(struct FCFS*);

#endif /* lab8Func_h */



int main( void )
{
    struct FCFS jobs;

    readFile(&jobs);

    printf("%c", jobs.jobList[0].processName);
} // end function: main


void readFile(struct FCFS *jobs)
{
    FILE *file = NULL;

    if((file = fopen("/Users/Vin/desktop/PAS.txt","r")) == NULL)
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    for(int i=0 ; i < MAX_RECORDS; i++)
    {
        char temp;
        if( 1 != fscanf(file," %c", &temp ) )
        { // handle error and exit
             perror( "fscanf failed" );
             fclose( file );  // cleanup
             exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        jobs->jobList[i].processName = temp;

        // finish inputting the current line from the file
        int ch;
        while( (ch = getc( file ) ) != EOF && '\n' != ch )
        {
            ;
        }
    }

    fclose( file );   // cleanup
}  // end function:  readFile

请记住一件事,您应该始终查看fscanfscanfchecking其返回的是true还是false(任何整数值或0),即它是否以正确的格式读取,然后进行进一步处理。

您需要在readFile函数的for循环中修改代码:

   if((check=fscanf(file," %c", &temp))>0) 
      process1->jobList[i].processName = temp;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM