私の理解するために、すべての行の文字列の長さを知らなくても、fscanfは使用方法、解析する方法があり、入力よう:は
000000 $出口$$$ 16
Cheitとそのお仕置き$$$ 8
戦争とサッカー$$$ 12の
超短パルス $$$ 8
非線形光学$$$ 8
などを再生するにはどのように戦争$$$ 12
の記念$$$ 12の
風..
"$$$"はデータのフィールド間を区切ります。
私はフレーズアップグレードするために探しています:それはノーライン収まるでしょう
sscanf(line, " %200[^$][^$][^$]$$$%ld", name, &copies);
を。 1である。
EDIT:
#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;
}
通常のアプローチが使用することである
strstr()
がstring.h
であることに注意してください'fgets'を使って行全体を適切な大きなバッファに読み込み、' sscanf'/'strtok'/whateverを使いますそれを解析する。 –試しましたか?提示するコードはありますか? – Gam
*最大*行の長さはありますか?そして、私は 'fscanf'(およびファミリー)が正しい選択であるかもしれないと確信していません...代わりに、おそらく' fgets'と 'strstr'で何かをするでしょう。 –