簡體   English   中英

為什么該程序無法在我的文件上打印?

[英]Why is this program not printing on my file?

該代碼應該將用戶輸入的信息打印到文件上,但是它所做的只是創建一個空文件...

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

struct room
{
    int room;
    char type[9];
    int cap;
    int price;
}rm;
FILE *k;
int main(){
    struct room rm;
    k=fopen("rooms.txt","w");
    printf("Please enter room number:");
    scanf("%d", rm.room);
    printf("\nPlease enter a description:");
    scanf("%s", rm.type);
    printf("\nPlease enter the room capacity:");
    scanf("%d", rm.cap);
    printf("\nPlease enter the price:");
    scanf("%d", rm.price);
    fprintf(k,"%d\t %s\t %d\t %d\n", rm.room,rm.type,rm.cap,rm.price);
    fclose(k);
}

這里

struct room
{
    int room;
    char type[9];
    int cap;
    int price;
}rm;

rm.roomrm.caprm.price均為int類型,在掃描用戶輸入時,您需要o提供地址&在其中存儲一個整數。 例如替換

scanf("%d", rm.room); /* to store something into rm.room need to provide address */

scanf("%d", &rm.room);

和這個

scanf("%d", rm.cap); /* address is not provided */
scanf("%d", rm.price); /* address is not provided */

scanf("%d", &rm.cap);
scanf("%d", &rm.price);

還要檢查fopen()的返回類型。 例如

k=fopen("rooms.txt","w");
if(k == NULL) {
 /* @TODO error handling */
 fprintf(stderr, "failure message\n");
 return 0;
}

以下建議的代碼:

  1. 干凈地編譯
  2. 將評論納入問題
  3. 執行所需的功能
  4. 不包括頭文件,不使用那些內容
  5. 正確檢查錯誤
  6. 兌現印刷頁的右邊距
  7. 避免任何可能的緩沖區溢出和由此產生的未定義行為
  8. 記住對數組的引用會降級為數組第一個字節的地址
  9. 始終將地址作為參數傳遞給函數: scanf()
  10. 說明為什么包含每個頭文件
  11. 將結構的定義與結構的實例分開
  12. 沒有聲明被同名結構的另一個實例“陰影”的結構實例
  13. 使用功能的有效簽名之一: main()

現在,建議的代碼為:

#include <stdio.h>   // fprintf(), fopen(), fclose(), perror(), scanf()
#include <stdlib.h>  // EXIT_FAILURE, exit()

struct room
{
    int room;
    char type[9];
    int cap;
    int price;
};


int main( void )
{
    struct room rm;

    FILE *k = fopen("rooms.txt","w");
    if( !k )
    {
        perror( "fopen rooms.txt for writing failed" );
        exit( EXIT_FAILURE );
    }

    printf("Please enter a room number:");
    if( scanf("%d", &rm.room) != 1 )
    {
        fprintf( stderr, 
                 "scanf for -room number- failed\n" );
        fclose( k );
        exit( EXIT_FAILURE );
    }

    printf("\nPlease enter a room description:");
    if( scanf("%8s", rm.type) != 1 )
    {
        fprintf( stderr, 
                 "scanf for -room description- failed\n" );
        fclose( k );
        exit( EXIT_FAILURE );
    }

    printf("\nPlease enter the room capacity:");
    if( scanf("%d", &rm.cap) != 1 )
    {
        fprintf( stderr, 
                 "scanf for -room capacity- failed\n" );
        fclose( k );
        exit( EXIT_FAILURE );
    }

    printf("\nPlease enter the room price:");
    if( scanf("%d", &rm.price) != 1 )
    {
        fprintf( stderr, 
                 "scanf for -room price- failed\n" );
        fclose( k );
        exit( EXIT_FAILURE );
    }

    // multi lined parameters to honor right page margin
    fprintf(k,"%d\t %s\t %d\t %d\n", 
        rm.room,
        rm.type,
        rm.cap,
        rm.price);
    fclose(k);
}

回答OP的問題: 為什么該程序無法在我的文件上打印?

由於發布的代碼未輸入任何內容,這是由於scanf()函數參數的語法不正確

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM