[英]***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讀取循環內的所有三列。
建議的代碼如下:
stderr
而不是stdout
fscanf()
盡管更好的調用是fgets()
,因為它將消除包含getc()
的代碼塊 main()
函數使用正確的簽名 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
請記住一件事,您應該始終查看fscanf
或scanf
來checking
其返回的是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.