[英]How to read in 3 input values from a file and read them sequentially?
我正在嘗試從第一行的文本文件( in.txt
)中讀取三個3值。 我如何在C中實現呢?
文本文件的第一行包含3個數字( N, S, D
)。 它們代表文件中輸入單詞的順序。 因此,例如,我的in.txt文件為:
8 3 5
mary
tom
jane
joe
dave
judy
fred
bill
jane
jones
judy
mary
judy
fred
joe
dave
因此,前8
將對應於N
,后3
對應於S
,后5
於D
這樣做的目的是讓我將前8
添加到Binary Search Tree中,然后將3
到BST中的Search中,將后5
到BST中。
但是,為此,我想知道的是如何能夠從文件中讀取它們以啟動BST。
我還沒走很遠,我只能打開文件,但由於它根本無法工作,不得不刪除其他代碼。 所以這就是我現在所擁有的。
int main()
{
FILE *inFile, *outFile;
int n, s, d;
inFile = fopen("in.txt", "r");
while(!feof (inFile))
{
fscanf(inFile, "%d %d %d", &n, &s, &d);
}
fclose(inFile);
}
這是我在Stackoverflow上的第一篇文章,非常感謝您的幫助!
首先,您將需要回顧為什么while(!feof(file))總是錯誤?
從文件中讀取數據行時 ,請使用面向行的輸入函數(例如fgets()
或POSIX getline()
將整行讀取到緩沖區中,然后從緩沖區中解析所需的值。 這提供了兩個主要好處:
您可以獨立驗證:
一種。 從文件中讀取數據; 和
灣 從行中解析值。
輸入緩沖區中剩余的內容不取決於使用的scanf
格式說明符 。 (總是消耗整行,因此您隨時可以從下一行的開頭開始閱讀以供下一次輸入)
每當您從文件讀取時,只需在使用fgets
時提供足夠大小的緩沖區即可(不要跳過緩沖區大小!)。 POSIX getline
將自動分配所需的任何空間。 然后使用sscanf
解析行中所需的任何信息(或strtok
或strsep
或您想要在緩沖區中定位特定點的任何其他函數)。
如果您不解析信息而需要整行,只需用nul-character覆蓋緩沖區的末尾來修剪'\\n'
。 strcspn
提供了一種簡單的方法,或者使用strlen
來獲取長度,然后覆蓋最后一個字符。
在您的情況下,您只需要以不同於其余行的方式處理第一行-因此,請將行計數器在開始時初始化為零。 如果您的行計數器為零,則解析該行的3整數值,否則對后續行進行任何所需的操作(下面的代碼與行號一起簡單地輸出)
綜上所述,您可以執行以下操作:
#include <stdio.h>
#include <string.h>
#define MAXC 1024 /* don't skimp on buffer size */
int main (int argc, char **argv) {
char buf[MAXC]; /* buffer to hold each line */
int n, s, d; /* your int variables */
size_t ndx = 0; /* line counter */
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
while (fgets (buf, MAXC, fp)) { /* read each line into buf */
if (!ndx) { /* if 1st line, parse into n, s, d */
if (sscanf (buf, "%d %d %d", &n, &s, &d) == 3)
printf ("line[%2zu] - n: %d s: %d d: %d\n",
ndx + 1, n, s, d);
else { /* if parse fails - handle error */
fputs ("error: invalid format - line 1.\n", stderr);
return 1;
}
}
else { /* for all subsequent lines */
buf[strcspn(buf, "\r\n")] = 0; /* trim '\n' from buf */
printf ("line[%2zu] : %s\n", ndx + 1, buf);
}
ndx++; /* increment line counter */
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
return 0;
}
使用/輸出示例
使用輸入文件,您將獲得以下內容:
$ ./bin/rdline1_3int dat/line1_3int.txt
line[ 1] - n: 8 s: 3 d: 5
line[ 2] : mary
line[ 3] : tom
line[ 4] : jane
line[ 5] : joe
line[ 6] : dave
line[ 7] : judy
line[ 8] : fred
line[ 9] : bill
line[10] : jane
line[11] : jones
line[12] : judy
line[13] : mary
line[14] : judy
line[15] : fred
line[16] : joe
line[17] : dave
我會做這樣的事情:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main()
{
int fd, N, S, D;
char buff[1];
fd = open("in.txt", O_RDONLY);
//reading N
if(read(fd, buff, 1) == -1){
perror("Unable to read N");
exit(0);
}
//converting N to int value
//EDIT
N = atoi(buff);
//skipping the file descriptor to avoid reading the space
lseek(fd, 1, SEEK_CUR);
//reading S
if(read(fd, buff, 1) == -1){
perror("Unable to read S");
exit(0);
}
//converting S to int value
//EDIT
S = atoi(buff);
//skipping the file descriptor to avoid reading the space
lseek(fd, 1, SEEK_CUR);
//reading D
if(read(fd, buff, 1) == -1){
perror("Unable to read N");
exit(0);
}
//converting D to int value
//EDIT
D = atoi(buff);;
close(fd);
//here your values are initialized
}
我沒有測試此代碼,因此它可能無法正常工作,請讓我知道它是否有效:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.