Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
写入文件时转储的C程序内核_C_Pointers_Struct_Io_Coredump - Fatal编程技术网

写入文件时转储的C程序内核

写入文件时转储的C程序内核,c,pointers,struct,io,coredump,C,Pointers,Struct,Io,Coredump,我是C程序的新手,我正在编写一个程序,该程序正在将一组结构写入一个.txt文件 这是我的尝试: #include <stdio.h> struct FileSig { char name[256]; char mode; char user_id; char group_id; char size; char time_last_mod[50]; }; int main(int argc, char *

我是C程序的新手,我正在编写一个程序,该程序正在将一组结构写入一个.txt文件

这是我的尝试:

#include <stdio.h>

struct FileSig {
    char name[256];  
    char mode;   
    char user_id;   
    char group_id;   
    char size;  
    char time_last_mod[50]; 
};


int main(int argc, char **argv)
{
    struct FileSig FileSig1;
    FILE *fp_Out;
    fp_Out = fopen( "out.txt" , "w" );

    printf("Enter name: ");
    scanf("%s", FileSig1.name);
    fprintf(fp_Out, "Name: %s\n", FileSig1.name);

    printf("Enter mode: ");
    scanf("%s", FileSig1.mode);
    fprintf(fp_Out, "Mode: %s\n", FileSig1.mode); 

    printf("Enter user id: ");
    scanf("%s", FileSig1.user_id);
    fprintf(fp_Out, "userID: %s\n", FileSig1.user_id); 

    printf("Enter group id: ");
    scanf("%s", FileSig1.group_id); 
    fprintf(fp_Out, "groupID: %s\n", FileSig1.group_id); 

    printf("Enter size: ");
    scanf("%s", FileSig1.size);
    fprintf(fp_Out, "size: %s\n", FileSig1.size);  

    printf("Enter time last modifly: ");
    scanf("%s", FileSig1.time_last_mod);
    fprintf(fp_Out, "time_last_mod: %s\n", FileSig1.time_last_mod);  

//  fp_Out = fopen( "out.txt" , "w" );
//  fprintf(fp_Out, "Name: %s\nMode: %d\nUserID:%d\nGroupID: %d\nSize:%d\nTime last modifly:%s", FileSig1.name, FileSig1.mode, FileSig1.user_id, FileSig1.group_id, FileSig1.size, FileSig1.time_last_mod);

    fclose(fp_Out);

    return 0;
}
#包括
结构文件{
字符名[256];
字符模式;
字符用户标识;
字符组标识;
煤焦尺寸;
char time_last_mod[50];
};
int main(int argc,字符**argv)
{
结构文件SIG FileSig1;
文件*fp_Out;
fp_Out=fopen(“Out.txt”,“w”);
printf(“输入名称:”);
scanf(“%s”,FileSig1.name);
fprintf(fp_Out,“名称:%s\n”,FileSig1.Name);
printf(“进入模式:”);
scanf(“%s”,FileSig1.mode);
fprintf(fp_Out,“模式:%s\n”,FileSig1.Mode);
printf(“输入用户id:”);
scanf(“%s”,FileSig1.user\u id);
fprintf(fp\u Out,“用户id:%s\n”,FileSig1.user\u id);
printf(“输入组id:”);
scanf(“%s”,FileSig1.group\u id);
fprintf(fp\u Out,“组id:%s\n”,FileSig1.group\u id);
printf(“输入大小:”);
scanf(“%s”,FileSig1.size);
fprintf(fp_Out,“大小:%s\n”,FileSig1.size);
printf(“输入上次修改的时间:”);
scanf(“%s”,FileSig1.time\u last\u mod);
fprintf(fp\u Out,“时间最后一个模块:%s\n”,FileSig1.time\u最后一个模块);
//fp_Out=fopen(“Out.txt”,“w”);
//fprintf(fp\u Out,“名称:%s\n模式:%d\n用户id:%d\n组id:%d\n大小:%d\n上次修改时间:%s”,FileSig1.Name,FileSig1.mode,FileSig1.user\u id,FileSig1.group\u id,FileSig1.size,FileSig1.time\u-mod);
fclose(fp_Out);
返回0;
}
当我在第二次输入后运行它时,它说是分段错误(内核转储)

还有人能帮我吗? 这方面我是新手,我想学习。

“%s”格式说明符用于字符串。但是
模式
是一个字符。对字符使用“%c”。另外,将要存储字符的地址传递给scanf,而不是未初始化的值。

“%s”格式说明符用于字符串。但是
模式
是一个字符。对字符使用“%c”。另外,将要存储的字符的地址传递到
scanf
,而不是未初始化的值。

%s”表示字符串,但
模式
的类型为
char
,因此应使用“%c”,如下所示:

scanf("%c", &FileSig1.mode);
fprintf(fp_Out, "Mode: %c\n", FileSig1.mode); 
注意我是如何获取
模式的地址的,因为
scanf()
将指针作为参数

PS:您需要更改所有读取字符而不是字符串的scanf和printf,以遵循此建议。

“%s”用于字符串,但
模式
的类型为
char
,因此您应该使用“%c”,如下所示:

scanf("%c", &FileSig1.mode);
fprintf(fp_Out, "Mode: %c\n", FileSig1.mode); 
注意我是如何获取
模式的地址的,因为
scanf()
将指针作为参数


PS:您需要更改所有读取字符而不是字符串的scanf和printf以遵循此建议。

您的
struct FileSig
cange single char到char数组。您的
struct FileSig
cange single char到char数组的single char char成员。@alanchung欢迎您的好消息!在这种情况下,接受答案。=)@阿兰张:欢迎你,好消息!在这种情况下,接受答案。=)