简体   繁体   English

为什么该程序无法在我的文件上打印?

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

This code is supposed to print the information entered by the user onto a file, but all it does is create an empty 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);
}

Here 这里

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

rm.room , rm.cap and rm.price are of int type, while scanning input from user, you need o provide the address & to store an integer into it. rm.roomrm.caprm.price均为int类型,在扫描用户输入时,您需要o提供地址&在其中存储一个整数。 For eg Replace this 例如替换

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

with

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

and this 和这个

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

with

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

Also check the return type of fopen() . 还要检查fopen()的返回类型。 for eg 例如

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

the following proposed code: 以下建议的代码:

  1. cleanly compiles 干净地编译
  2. incorporates the comments to the question 将评论纳入问题
  3. performs the desired functionality 执行所需的功能
  4. does not include header files those contents are not used 不包括头文件,不使用那些内容
  5. properly checks for errors 正确检查错误
  6. honors the printed page right margin 兑现印刷页的右边距
  7. avoids any possibility of a buffer overflow and the resulting undefined behavior 避免任何可能的缓冲区溢出和由此产生的未定义行为
  8. remembers that a reference to an array degrades to the address of the first byte of the array 记住对数组的引用会降级为数组第一个字节的地址
  9. always passes an address as a parameter to function: scanf() 始终将地址作为参数传递给函数: scanf()
  10. documents why each header file is included 说明为什么包含每个头文件
  11. separates the definition of a struct from an instance of a struct 将结构的定义与结构的实例分开
  12. does not declare an instance of the struct that is 'shadowed' by another instance of the struct with the same name 没有声明被同名结构的另一个实例“阴影”的结构实例
  13. uses one of the valid signatures for function: main() 使用功能的有效签名之一: main()

and now, the proposed code: 现在,建议的代码为:

#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);
}

in answer to the OPs question: Why is this program not printing on my file? 回答OP的问题: 为什么该程序无法在我的文件上打印?

Because nothing is being input by the posted code, due to the incorrect syntax for the scanf() function parameters 由于发布的代码未输入任何内容,这是由于scanf()函数参数的语法不正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM