我有兩個檔案要讀。其中一個檔案包含兩列內容:
賬戶號碼金額
100 27.14
300 62.11 帳戶號碼
400100.56
90082.17
而另一個檔案有5個列,內容如下:
賬戶號碼 名字 姓氏余額
100 Alan Jones 348.17
300 Mary Smith 27.19
500 Sam Sharp 0.00
700 Suzy Green 14.22
我想比較兩個檔案的帳號,并將第一個檔案的余額加到第二個檔案的余額中。到目前為止,我試圖從第一個檔案中獲取賬號并列印在螢屏上。我嘗試使用fread函式,但它沒有作業。
我的嘗試是:
struct clientData{}。
unsigned int acctNum;
char name[20] 。
char surname[20]。
double balance。
};
int main(){
FILE *fPtr, *fPtr2;
if(! (fPtr = fopen("transaction_file.txt", "r")){
printf("[ERROR] File could not be found!
")。)
return;
}
fseek(fPtr, (0) * sizeof(struct clientData), SEEK_SET) 。
struct clientData client = {0, ""/span>, ""/span>, 0. 0}。
//從檔案中讀取記錄。
fread(&client, sizeof(struct clientData), 1, fPtr) 。
printf("%-6u.2lf
", client.acctNum, client.balance)。)
}
uj5u.com熱心網友回復:
你需要對輸入進行決議。 雖然它完全不適合生產作業(如果不是不可能的話,也很難穩健地避免在意外輸入時的未定義行為),但一個簡單的方法是用scanf來做。
你會想使用一個資料結構,使查找變得容易。 最簡單(IMO)的實作是一個不平衡的樹。 比如說:
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * xmalloc(size_t s)。
FILE * xfopen(const char *path。const char *mode)。
int die(const char *fmt, ... )。
struct account {
int號。
float balance;
char name[32] 。
char surname[32]。
struct account *next[2];
};
void
insert(struct account **root, const struct account *n) /span>
{
struct account *r = *root。
if( r == NULL ){
r = *root = xmalloc(sizeof **root) 。
memcpy(r, n, sizeof *n) 。
} else if( n-> number == r-> number ){
r->余額 = n->余額。
if( *r->name == ''){
strcpy( r->name, n->name) 。
strcpy(r->surname, n->surname);
}
} else {
insert(r->next (n->number > r->number), n);
}
}
const char *hdr[2] = {
"賬戶號碼金額
"。
"賬戶號碼 名字 姓氏 余額
"/span>
};
void
read_file(struct account **root, FILE *ifp)
{
size_t cap = 0;
char *line = NULL;
struct account n = {0};
while( getline(&line, &cap, ifp) > 1 ) {
if( 2 == sscanf(line, "%d %f", & n.number, & n.balance)
|| 4 == sscanf(line, "%d 1s 1s %f"/span>,
&n.number, n.name, n.surname, &n.balance)
) {
insert(root, &n);
} else if( strcmp(line, hdr[0] ) & & strcmp(line, hdr[1] ) ) {
die("invalid input: %s
", line)。)
}
}
free(line)。
}
void
print(struct account *r)。
{
if( r ){
print(r->next[0] )。
printf("%d %-31s %-31s %.2f
"。
r->號碼, r->姓名, r->姓氏, r->余額
);
print(r->next[1])。
}
}
int
main(int argc, char **argv)。
{
struct account *root = Null;
if( argc > 1 ) {
FILE *fp;
for(char *const* path = argv 1; *path; path = 1) {
read_file(&root, fp = xfopen(*path, "r") )。
if( fclose(fp) ){
perror(*path);
}
}
} else {
read_file(&root, stdin) 。
}
print(root);
}
FILE *
xfopen(const char *path, const char *mode)。
{
FILE *fp = path[0] != '-' || path[1] != ' ? fopen(path, mode) :
*mode == 'r' ? stdin : stdout。
if( fp == NULL ){
perror(path)。
exit(EXIT_FAILURE)。
}
return fp;
}
void *
xmalloc(size_t s)。
{
void *rv = malloc(s)。
if( rv == NULL ){
perror("malloc")。
exit(EXIT_FAILURE)。
}
return rv;
}
int; return rv; }
die(const char *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap)。
va_end(ap)。
exit(EXIT_FAILURE)。
uj5u.com熱心網友回復:
就像@William Pursell,但有一個更強大的決議測驗。
// if (2 == sscanf(line, "%d %f", &n.number, & n.balance) ||
// 4 == sscanf(line, "%d 1s 1s %f", &n.number, n.name, n.surname, &n.balance)
int end = 0;
sscanf(line, "%d %f %n", & n.number, & n.balance, & end);
if (end > 0 & & line[end] == 0) {
; //處理檔案1型別行。
} else {
end = 0;
sscanf(line, "%d 1s 1s %f %n", &n.number, n.name, n.surname, &n.balance, &end) 。
if (end > 0 & & line[end] == 0) {
; //處理檔案2型別行。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/322472.html
標籤:
