无法向代码添加正确的错误检查
Trouble adding correct error checks to code
对于我的作业,我得到了一个程序来添加错误检查,我很快就添加了前两个(我相信我已经解决了后面带有 X 的任何评论)但是我相信这个错误检查的问题第 60 行是 fscanf
愿意将 chars 读入需要整数的内容,因此我需要添加一些内容以打印出错误并在尝试读入 char 时停止程序。我也不确定在 create_graph
和 read_edge
中要检查什么错误。要读入此程序的文件格式如下:
4 5
1 2 0.2
2 3 0.3
3 4 -3.7
1 4 0.2
3 1 0.4
我最近的尝试是:
if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
printf("Error: Expected an Integer");
return 0;
}
当前代码:
to try and scan the input to make sure they're integers.
// missing error check (you may need to modify the function's return
// value and/or parameters)
edge read_edge(FILE* file) {
edge e;
fscanf(file, "%d %d %f", &e.source, &e.target, &e.weight);
return e;
}
graph create_graph(int n, int m) {
graph g = {
.n = n,
.m = m,
.vertices = calloc(n, sizeof(vertex)),
.edges = calloc(m, sizeof(edge)),
};
for(int i = 0; i < n; i++) {
g.vertices[i] = i + 1;
}
return g;
}
int main(int argc, char* argv[]) {
// missing error check -- related to argc/argv X
if (argv[2] != '[=12=]')
{
printf("Wrong number of arguments.\n");
return 0;
}
// missing error check (errno) X
FILE* file = fopen(argv[1], "r");
if (file == NULL) {
printf("Unable to open file: %s\n", strerror(errno));
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int n, m;
// missing error check
fscanf(file, "%d %d", &n, &m);
if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
printf("Error: Expected an Integer");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
graph g = create_graph(n, m);
for (int i = 0; i < m; i++) {
// missing error check (after you fix read_edge)
g.edges[i] = read_edge(file);
}
printf("%d %d\n", g.n, g.m);
return 0;
}
就像现在一样,程序在尝试读取文件时崩溃。
如何检查错误:
fscanf(file, "%d %d", &n, &m);
建议:
if( fscanf(file, "%d %d", &n, &m) != 2 )
{
fprintf( "fscanf of first line from the input file failed\n );
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
注意:scanf()
函数族 returns 成功输入格式转换(或 EOF)的次数
关于:
if (argv[2] != '[=12=]')
{
printf("Wrong number of arguments.\n");
return 0;
}
在讨论命令行参数问题时,最好显示(在stderr
上)一个USAGE语句,类似于:
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s <inputFileName>\n", argv[0] );
exit( EXIT_FAILURE );
}
// implied else, correct number of command line parameters
注意:argv[0]
始终是可执行文件的名称
对于我的作业,我得到了一个程序来添加错误检查,我很快就添加了前两个(我相信我已经解决了后面带有 X 的任何评论)但是我相信这个错误检查的问题第 60 行是 fscanf
愿意将 chars 读入需要整数的内容,因此我需要添加一些内容以打印出错误并在尝试读入 char 时停止程序。我也不确定在 create_graph
和 read_edge
中要检查什么错误。要读入此程序的文件格式如下:
4 5
1 2 0.2
2 3 0.3
3 4 -3.7
1 4 0.2
3 1 0.4
我最近的尝试是:
if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
printf("Error: Expected an Integer");
return 0;
}
当前代码:
to try and scan the input to make sure they're integers.
// missing error check (you may need to modify the function's return
// value and/or parameters)
edge read_edge(FILE* file) {
edge e;
fscanf(file, "%d %d %f", &e.source, &e.target, &e.weight);
return e;
}
graph create_graph(int n, int m) {
graph g = {
.n = n,
.m = m,
.vertices = calloc(n, sizeof(vertex)),
.edges = calloc(m, sizeof(edge)),
};
for(int i = 0; i < n; i++) {
g.vertices[i] = i + 1;
}
return g;
}
int main(int argc, char* argv[]) {
// missing error check -- related to argc/argv X
if (argv[2] != '[=12=]')
{
printf("Wrong number of arguments.\n");
return 0;
}
// missing error check (errno) X
FILE* file = fopen(argv[1], "r");
if (file == NULL) {
printf("Unable to open file: %s\n", strerror(errno));
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int n, m;
// missing error check
fscanf(file, "%d %d", &n, &m);
if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
printf("Error: Expected an Integer");
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
graph g = create_graph(n, m);
for (int i = 0; i < m; i++) {
// missing error check (after you fix read_edge)
g.edges[i] = read_edge(file);
}
printf("%d %d\n", g.n, g.m);
return 0;
}
就像现在一样,程序在尝试读取文件时崩溃。
如何检查错误:
fscanf(file, "%d %d", &n, &m);
建议:
if( fscanf(file, "%d %d", &n, &m) != 2 )
{
fprintf( "fscanf of first line from the input file failed\n );
exit( EXIT_FAILURE );
}
// implied else, fscanf successful
注意:scanf()
函数族 returns 成功输入格式转换(或 EOF)的次数
关于:
if (argv[2] != '[=12=]')
{
printf("Wrong number of arguments.\n");
return 0;
}
在讨论命令行参数问题时,最好显示(在stderr
上)一个USAGE语句,类似于:
if( argc != 2 )
{
fprintf( stderr, "USAGE: %s <inputFileName>\n", argv[0] );
exit( EXIT_FAILURE );
}
// implied else, correct number of command line parameters
注意:argv[0]
始终是可执行文件的名称