2013-08-16 35 views
1

所以我有這樣的代碼:如何正確掃描從輸入線和寫一個輸出文件

#include <stdio.h> 
#include <stdlib.h> 

struct tree 
{ 
    char data; 
    struct tree *left; 
    struct tree *right; 
}; 


int findNode(char temp[], int x, int y, char val); 
struct tree *insert(int data); 


struct tree *binarytree(char inorder[], char preorder[], int x, int y) 
{ 
    static int index = 0; 

    if (x > y) 
     return NULL; 

    struct tree *new = insert(preorder[index++]); 


    if (x == y) 
     return new; 

    int inIndex = findNode(inorder, x, y, new->data); 

    new->left = binarytree(inorder, preorder, x, inIndex - 1); 
    new->right = binarytree(inorder, preorder, inIndex + 1, y); 

    return new; 
} 

int findNode(char temp[], int x, int y, char val) 
{ 
    int i; 

    for (i = x; i <= y; i++) 
    { 
     if (temp[i] == val) 
      return i; 
    } 
} 


struct tree *insert(int data) 
{ 
    struct tree *tree = (struct tree *)malloc(sizeof(struct tree)); 
    tree->data = data; 
    tree->left = NULL; 
    tree->right = NULL; 

    return (tree); 
} 

void postorder(struct tree *tree) 
{ 
    FILE *ofp; 
    ofp = fopen("201262480.txt", "w"); 

    if (tree == NULL) 
    { 
     return; 
    } 
    else 
    { 
     postorder(tree->left); 
     postorder(tree->right); 
     fprintf(ofp, "%d ", tree->data); 
    } 

} 

int main() 
{ 
    int i = 0, j; 
    int temp[100]; 
    char c, buffer[20]; 
    FILE *fp; 
    fp = fopen("input.txt", "r"); 

    if (fp != NULL) 
    { 
     while (1 == fscanf(fp, "%d ", &temp[i])) 
     { 
      i++; 
     } 

     char inorder[i/2]; 
     char preorder[i/2]; 

     for (j = 0; j < i/2; j++) 
     { 
      preorder[j] = temp[j]; 
      inorder[j] = temp[j + (i/2)]; 
     } 

     int length = sizeof(inorder)/sizeof(inorder[0]); 
     struct tree *root = binarytree(inorder, preorder, 0, length - 1); 
     postorder(root); 
    } 
    else 
    { 
     printf("Cannot open File!\n"); 
    } 

    return 0; 
} 

而且我有這樣的輸入文件:直到

1 2 3 4 5 6 7 
3 2 4 1 6 5 7 
*** 
1 1 2 
1 1 2 
***end of input*** 

它掃描了」 *符號。它處理數字並打印輸出文件。現在我有兩個問題。

  1. 我怎樣才能掃描下一行數字(1 1 2和1 1 2)並重復該過程。我可以使用什麼參數作爲while循環?
  2. 輸出文本文件只打印最後一個元素(這可以在後序功能中找到)。它應該打印:3 4 2 6 7 5 1 但是,它只打印1。但是,當我使用printf時,它會打印正確的輸出。

注:整個代碼工作。不需要編輯二叉樹和東西,它只是對整數進行掃描,並將輸出寫入文件,這讓我很生氣。

請幫忙!

+1

FCLOSE(OFP);對於初學者。 – Jiminion

+0

假設我想循環直到找到字符*,那麼代碼是什麼? –

+0

如果這些是行,那麼也許使用fgets()。 fgets()一次讀入一行。然後解析該行(查找*)並將其丟棄,如果它有一個或多個。 – Jiminion

回答

0

使用a接入模式,而不是w

w創建輸出操作的空文件。
每次撥打postorder()函數時,都會覆蓋201262480.txt文件。這就是爲什麼你的輸出是1postorder()函數的最後一個函數覆蓋201262480.txt並在那裏寫入1

a在文件末尾打開輸出文件。
使用此訪問模式,您將獲得預期的輸出。

void postorder(struct tree *tree) 
{ 
    FILE *ofp; 
    ofp = fopen("201262480.txt", "a"); // I've changed "w" to "a". 
             // Now the output will be proper. 
    if (tree == NULL) 
    { 
    return; 
    } 
    else 
    { 
     postorder(tree->left); 
     postorder(tree->right); 
     fprintf(ofp, "%d ", tree->data); 
    } 
    fclose(ofp); 
} 

產量預計:

3 4 2 6 7 5 1 

UPDATE:

我已經修改了你的postorder()功能,所以它會在您每次運行程序時改寫輸出文件。

下面是代碼:

void postorder(struct tree *tree) 
{ 
    unsigned int pointerValue = 0; 
    static int overwriteFile = 0; // The file hasn't been overwritten yet. 
    FILE *ofp = NULL; 
    ofp = fopen("201262480.txt", "a"); 

    fseek(ofp, 0L, SEEK_END);  // Moving the pointer to the end of file. 
    pointerValue = ftell(ofp);   // Checking if it is not `0` (the file contains some data) 
    if (pointerValue != 0) { 
     if (overwriteFile == 0) { 
      fclose(ofp); 
      ofp = fopen("201262480.txt", "w"); // "w": Create an empty file for output operations. 
      fclose(ofp); 
      ofp = fopen("201262480.txt", "a"); // "a": Open file for output at the end of a file. 
      overwriteFile = 1; // The file was overwritten. 
           // So when you call this function next time, 
           // it will not overwrite the file. 
           // It only does it once when you run the program. 
     } 
    } 

    if (tree == NULL) 
    { 
     return; 
    } 
    else 
    { 
     postorder(tree->left); 
     postorder(tree->right); 
     fprintf(ofp, "%d ", tree->data); 
    } 
    fclose(ofp); 
} 
+0

謝謝@Yulian。但是如何在每次運行程序時覆蓋輸出? –

+0

請參閱**更新**。 – yulian