使用升华文本和Xcode在Mac上进行文件输入/输出。C语言
所以我的第一个问题是,我如何使用Xcode和terminal在Mac上执行文件输入/输出程序 第二,在我弄明白之前,有没有人介意告诉我这是否正确,因为我目前无法编译和运行它?本周我将对我的硬盘进行分区,并在其上设置窗口,但在此之前,我想学习准备考试 这里是实践问题: 编写一个程序,从第一行读入一个整数n,然后在随后的n行上每行读入两件事(一个int SSN和一个float)。所有这些都是一个文件。必须以字符串形式提示输入文件名,该字符串的长度必须小于30个字符。打开文件等,读取所有内容,并保持所有工资收入的运行总数,然后在读取完成后,提示输入输出文件名字符串,打开它并将所有收入的平均值写入其中 这是我的密码:使用升华文本和Xcode在Mac上进行文件输入/输出。C语言,c,macos,io,C,Macos,Io,所以我的第一个问题是,我如何使用Xcode和terminal在Mac上执行文件输入/输出程序 第二,在我弄明白之前,有没有人介意告诉我这是否正确,因为我目前无法编译和运行它?本周我将对我的硬盘进行分区,并在其上设置窗口,但在此之前,我想学习准备考试 这里是实践问题: 编写一个程序,从第一行读入一个整数n,然后在随后的n行上每行读入两件事(一个int SSN和一个float)。所有这些都是一个文件。必须以字符串形式提示输入文件名,该字符串的长度必须小于30个字符。打开文件等,读取所有内容,并保持所
#include <stdlib.h>
#include <stdio.h>
FILE *F1, *F2;
void main () {
int SSN, n, i;
float wages, total;
char f1name, f2name;
scanf("%s", &f1name);
F1 = fopen("f1name", "r");
fscanf(F1,"%d", &n);
{
// Reading in the
for(i=0; i<n; i++)
{
fscanf(F1,"%d%f", &SSN, &wages);
total += wages;
}
// Scanning in file name and opening it
scanf("%s", &f2name);
F2 = fopen(fname, "w");
// Writing to the file the average of all earnings
fprintf(F2,"%d%f", SSN, total/n);
}
// Closing the file
fclose(F1);
fclose(F2);
#包括
#包括
文件*F1,*F2;
空干管(){
int-SSN,n,i;
浮动工资总额;
字符f1name,f2name;
scanf(“%s”,名称(&F);
F1=fopen(“F1名称”、“r”);
fscanf(F1、%d、&n);
{
//阅读
for(i=0;if1name
和f2name
应该是存储文件名的字符数组。您已将它们定义为字符,尝试在其中存储字符串将调用未定义的行为,因为scanf
将执行非法内存访问
此外,main
函数的签名应为以下任一项
int main(void);
int main(int argc, char *argv[]);
您应该修改您的程序以
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// variable name SSN change to lowercase
int ssn, n, i;
int retval; // to save the return value of fscanf
float wages, total;
char f1name[30+1], f2name[30+1];
// define file pointers inside main
// also change the name to lowercase
FILE *f1, *f2;
scanf("%30s", f1name);
f1 = fopen(f1name, "r");
// check for error in opening file
if(f1 == NULL) {
// print error message to stderr
perror("error in opening file\n");
// handle it
}
retval = fscanf(f1, "%d", &n);
if(retval != 1) {
perror("error in reading from the file\n");
// handle it
}
for(i = 0; i < n; i++) {
retval = fscanf(f1,"%d%f", &ssn, &wages);
if(retval != 2) {
perror("error in reading from the file\n");
// handle it
}
total += wages;
}
scanf("%30s", f2name);
f2 = fopen(f2name, "w");
// check for error in opening file
if(f2 == NULL) {
// print error message to stderr
perror("error in opening file\n");
// handle it
}
// Writing to the file the average of all earnings
fprintf(f2,"%d %f", ssn, total / n);
// Closing the file
fclose(f1);
fclose(f2);
return 0;
}
#包括
#包括
内部主(空){
//变量名SSN更改为小写
int-ssn,n,i;
int retval;//保存fscanf的返回值
浮动工资总额;
字符f1name[30+1],f2name[30+1];
//在main中定义文件指针
//同时将名称更改为小写
文件*f1,*f2;
scanf(“%30s”,f1名称);
f1=fopen(f1名称,“r”);
//检查打开文件时是否有错误
如果(f1==NULL){
//将错误消息打印到stderr
perror(“打开文件时出错”);
//处理它
}
retval=fscanf(f1、%d、&n);
如果(返回值!=1){
perror(“读取文件时出错”);
//处理它
}
对于(i=0;i
我认为您看起来是+1,但如果在标准错误中报告错误消息而不是在标准输出中报告则更好。它还应该检查每个fscanf()
call返回预期的值数;如果不是,则可能是报告错误并退出的时候了。根据一般经验,从标准输入中读取文件名而不是将其作为程序的参数是次优的,但我意识到这是给您的设计。@JonathanLeffler做了您建议的更改谢谢你:)