2011-04-27 83 views
0

如何創建一個輸入函數來打開輸入文件並從文件讀入數據?並且該文件是否必須存儲在某個位置?我可以這樣做,只需將它保存在我的桌面上作爲文本文件?這是我到目前爲止的內容創建一個輸入函數

#include <stdio.h> /* NULL is defined here */ 
#include <stdlib.h> /* for malloc() */ 
#include <string.h> /* for string related functions */ 

#define NAME_LEN 10 

struct data { 
    char name[NAME_LEN]; 
    int age; 
    int weight; 
}; 

typedef struct data DATA; 

struct linked_list { 
    DATA    d; 
    struct linked_list * next; 
}; 

typedef struct linked_list ELEMENT; 
typedef ELEMENT *   LINK; 

/* function prototypes */ 
LINK create_list_from_file(const char *); 
void print_list_to_file(LINK, const char *, const char *); 
int count(LINK, const int, const int); 
LINK lookup(const DATA, LINK); 
void insert(LINK, LINK, LINK); 

nt main(int argc, char *argv[]) 
{ 
    LINK head, found, to_add; 
    char *in_file_name = "infile"; /* default input file name */ 
    char *out_file_name = "outfile"; /* default output file name */ 
    int a = 20, w = 150; /* will be used as the age limit and weight limit */ 


    /* The input and output files can be given as command line arguments */ 
    switch (argc) { 
    case 1: 
     printf("The default input and output files are %s and %s.\n", 
       in_file_name, out_file_name); 
     break; 
    case 2: 
     printf("The input file is %s and the default output file is %s.\n", 
       argv[1], out_file_name); 
     in_file_name = argv[1]; 
     break; 
    case 3: 
     printf("The input file is %s and the output file is %s.\n", 
       argv[1], argv[2]); 
     in_file_name = argv[1]; 
     out_file_name = argv[2]; 
     break; 
    default: 
     printf("The input file is %s and the output file is %s.\n", 
       argv[1], argv[2]); 
     in_file_name = argv[1]; 
     out_file_name = argv[2]; 
     printf("The remaining arguments are not used.\n"); 
    } 

/* 
1. invoke create_list_from_file() function to create a linear linked list 
    from the data in the input file, 
*/ 
    head = create_list_from_file(in_file_name); 
/* 
2. invoke print_list_to_file() function with the writing mode ("w"), 
*/ 
    print_list_to_file(head, out_file_name, "w"); 
/* 
3. invoke the count() function, 
4. output the counted result to the screen, 
*/ 
    printf("The number of people with age over %d and weight over %d is %d.\n", 
      a, w, count(head, a, w)); 
/* 
5. invoke the lookup() function and insert() function, and 
*/ 
    /* prepare an element to be looked-up and added */ 
    to_add = malloc(sizeof(ELEMENT)); 
    strcpy(to_add -> d.name, "Janet"); 
    to_add -> d.age = 21; 
    to_add -> d.weight = 150; 
    to_add -> next = NULL; 

    found = lookup(to_add -> d, head); 
    insert(head, found, to_add); 
/* 
6. invoke print_list_to_file() function with the append mode ("a"). 
*/ 
    print_list_to_file(head, out_file_name, "a"); 

/* 
repeat step 5 with an element that does not exist in the current list 
*/ 
    /* prepare an element to be looked-up and added */ 
    to_add = malloc(sizeof(ELEMENT)); 
    strcpy(to_add -> d.name, "Jerry"); 
    to_add -> d.age = 24; 
    to_add -> d.weight = 220; 
    to_add -> next = NULL; 

    found = lookup(to_add -> d, head); 
    insert(head, found, to_add); 
/* 
repeat step 6: invoke print_list_to_file() function with the append mode ("a"). 
*/ 
    print_list_to_file(head, out_file_name, "a"); 

    return 0; 
} 
+0

該文件不必位於特定位置。只是谷歌「C文件閱讀示例」,你會發現很多的例子。上面的代碼與讀取文件無關。 – 2011-04-27 04:39:26

+0

對不起,發佈了錯誤代碼 – Jacob 2011-04-27 04:42:46

+0

@jonsca:不是這樣,'in_file_name'是一個'char *',並且可以指向'argv [1]'。 – 2011-04-27 04:51:38

回答

0

您可以使用標準庫打開計算機上幾乎任何文件,包括可訪問的網絡存儲。可以讀取文本或二進制文件,包括簡單的「.txt」文件。例如,您可以從文件中讀取一個整數:

#include <stdio.h> 
... 
int i; 
FILE *f; 
f= fopen("D:\Temp\SomeFile.txt", "r"); 
fscanf(f, "%d", &i); 
fclose(f); 

您可以使用面向行的語義,如果你想(如上面的例子) - look C library reference for fopen等等應該有一些例子,它們很有用也。

+0

這會失敗,你需要在將它們嵌入到字符串文字中時避開反斜槓。 – unwind 2011-04-27 07:22:35