據我瞭解字符串的長度,還有解析的方式輸入這樣的:如何使用的fscanf不知道在每一行
一億$出口$$$ 16
的Cheit及其處罰$$$ 8
的戰爭和記憶$$$ 12個
風戰爭$$$ 12
如何玩足球$$$ 12個
超短脈衝 8個$$$
非線性光學$$$ 8
等。
「$$$」在數據字段之間分開。
我期待升級了一句:
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
所以它會適合線上沒有。 1在這個例子中。
編輯:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME_LENGTH 200
#define ERROR -1
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef struct book{
char name[NAME_LENGTH];
long copies;
struct book *next;
} Book;
Book* create_book(char name[], long copies){
Book *new_book = (Book*) malloc(sizeof(Book));
if (new_book != NULL) {
strcpy(new_book->name, name);
new_book->next = NULL;
new_book->copies = copies;
}
return new_book;
}
Book* add_first(Book *head, char name[], long copies){
Book *new_book = create_book(name, copies);
if (new_book == NULL)
return NULL;
new_book->next = head;
return new_book;
}
Book* add_last(Book *head, char name[], long copies){
Book *tail;
Book *new_book = create_book(name, copies);
if (new_book == NULL)
return NULL;
if (head == NULL)
return new_book;
tail = head;
while (tail->next != NULL)
tail = tail->next;
tail->next = new_book;
return head;
}
Book* add_sorted(Book *head, char name[], long copies){
Book* iter, *prev = NULL;
Book* new_book = create_book(name, copies);
if(new_book == NULL)
return head;
if (head == NULL)
return new_book;
if (!strcmp(new_book->name, head->name)){
new_book->next = head;
return new_book;
}
iter = head;
while ((iter != NULL) && (strcmp(new_book->name, head->name))){
prev = iter;
iter = iter->next;
}
prev->next = new_book;
new_book->next = iter;
return head;
}
int length(const Book *head){
if (head == NULL)
return 0;
return 1 + length(head->next);
}
void free_library(Book *head_book){
if (head_book == NULL)
return;
free_library(head_book->next);
free(head_book);
}
Book* find_book(Book *head, char name[]){
if (head == NULL)
return NULL;
if (strcmp(head->name, name) == 0)
return head;
find_book(head->next, name);
return NULL;
}
Book* delete_book(Book *head, char name[]){
Book *iter = head, *prev = NULL;
if (head == NULL)
return head;
if ((!strcmp(head->name, name)) == 1){
iter = head->next;
free(head);
return iter;
}
while (iter->next != NULL){
if ((!strcmp(head->name, name)) == 1){
prev->next = iter->next;
free(iter);
break;
}
prev = iter;
iter = iter->next;
}
return head;
}
Book* initBooksList(FILE *input){
Book *head_book = NULL, *existing_book = NULL;
long copies = 0;
char line[256] = {0}, name[NAME_LENGTH];
if (input == NULL){
printf("File did not open. Exit..\n");
return NULL;
}
while(!feof(input)){
if((fgets(line, 256, input) != NULL) && (head_book == NULL)){
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
printf("%s\n%ld\n", name, copies);
head_book = create_book(name, copies);
strcpy(line, "");
strcpy(name, "");
copies = 0;
}
else{
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
existing_book = find_book(head_book, name);
if(existing_book != NULL){
existing_book->copies += copies;
printf("%s\n%ld\n", name, existing_book->copies);
}
else{
add_sorted(head_book, name, copies);
printf("%s\n%ld\n", name, copies);
strcpy(line, "");
strcpy(name, "");
copies = 0;
}
}
}
return head_book;
}
void storeBooks(Book *head_book){
}
void returnBook(Book *head_book){
}
void borrowBook(Book *head_book){
}
int main(int argc, char *argv[]){
int i = 0;
FILE *ptr;
printf("%d\n", argc);
for(i = 0; i < argc; i++)
printf("argv[%d] = %s\n", i, argv[i]);
ptr = fopen(argv[1], "r");
initBooksList(ptr);
return 0;
}
通常的方法是使用'fgets'將整行讀入合適的大緩衝區,然後使用'sscanf' /'strtok'/whatever解析它。 –
你試過了嗎?你有代碼要呈現嗎? – Gam
你有*最大*行長嗎?我不確定'fscanf'(和家庭)在這裏可能是正確的選擇......相反,我可能會用'fgets'和'strstr'做一些事情。 –