代码问题 [英] code question
问题描述
我有这个代码我想清理。
#include< stdio.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
double x,y,a,b;
FILE * fp;
x = strtod(argv [1],NULL);
y = strtod(argv [2],NULL);
a = strtod(argv [3],NULL);
b = strtod(argv [4],NULL);
if((fp) = fopen(argv [4],a))== NULL){
puts(fopen error);
exit(-1);
}
fprintf(fp,"%。2f \ t%。2f \ t%。2f \ t%。2f \ n",x ,y,a,b);
if(fclose(fp)== EOF){
puts(fclose error);
退出(-1);
}
返回0;
}
如果程序运行没有参数我得到一个seg错误。如果用
4运行,没问题。如果它运行少于四个(包括argv [0])那么
程序不想运行正确。我怎么能用这个
程序说一两个argv?
比尔
I have this code I would like to clean up.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double x, y, a, b;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
a = strtod(argv[3], NULL);
b = strtod(argv[4], NULL);
if ((fp = fopen(argv[4], "a")) == NULL) {
puts("fopen error");
exit(-1);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(-1);
}
return 0;
}
If the program is run with no arguments I get a seg fault. If it is run with
4, no problem. If it is run with less than four (that includes argv[0]) then
the program doesn''t want to run right. How would I be able to use this
program with say one or two argvs ?
Bill
推荐答案
Bill Cunningham说:
Bill Cunningham said:
我有这个代码我想清理。
#include< stdio.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
双x,y,a,b;
FILE * fp;
x = strtod(argv [ 1],NULL);
y = strtod(argv [2],NULL);
a = strtod(argv [3],NULL);
b = strtod(argv [4],NULL);
if((fp = fopen(argv [4]," a"))== NULL){
put(fopen error);
退出(-1);
}
fprintf(fp,"%。 2f \t%。2f \ t%。2f \ t%。2f \ n",x,y,a,b);
if(fclose(fp)== EOF ){
puts(fclose error);
退出(-1);
}
返回0;
}
如果程序运行时没有参数,我会遇到段错误。如果运行
4,没问题。如果它运行少于四个(包括
argv [0])那么程序不想运行正确。我怎么能用这个程序说一两个argv?
?
I have this code I would like to clean up.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double x, y, a, b;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
a = strtod(argv[3], NULL);
b = strtod(argv[4], NULL);
if ((fp = fopen(argv[4], "a")) == NULL) {
puts("fopen error");
exit(-1);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(-1);
}
return 0;
}
If the program is run with no arguments I get a seg fault. If it is run
with 4, no problem. If it is run with less than four (that includes
argv[0]) then the program doesn''t want to run right. How would I be able
to use this program with say one or two argvs ?
#include< stdio.h>
#include< stdlib.h>
#define DEFAULT_FILE_NAME" foo.bar"
int main(int argc,char * argv [])
{
double x = 0.0,y = 0.0,a = 0.0,b = 0.0;
const char * filename = DEFAULT_FILE_NAME;
FILE * fp = NULL;
if(argc 1)
{
x = strtod(argv [1],NULL);
}
if(argc 2)
{
y = strtod(argv [2],NULL);
}
if(argc 3)
{
a = strtod(argv [3],NULL);
}
if(argc 4)
{
b = strtod(argv [4],NULL);
filename = argv [4];
}
if((fp = fopen(filename," a"))== NULL){
puts(fopen error);
退出(EXIT_FAILURE);
}
fprintf(fp,"%。 2f\t%。2f \ t%。2f \ t%。2f \ n",x,y,a,b);
if(fclo se(fp)== EOF){
put(fclose error);
退出(EXIT_FAILURE);
}
返回0;
}
-
Richard Heathfield< http:// www。 cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_FILE_NAME "foo.bar"
int main(int argc, char *argv[])
{
double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
const char *filename = DEFAULT_FILE_NAME;
FILE *fp = NULL;
if(argc 1)
{
x = strtod(argv[1], NULL);
}
if(argc 2)
{
y = strtod(argv[2], NULL);
}
if(argc 3)
{
a = strtod(argv[3], NULL);
}
if(argc 4)
{
b = strtod(argv[4], NULL);
filename = argv[4];
}
if ((fp = fopen(filename, "a")) == NULL) {
puts("fopen error");
exit(EXIT_FAILURE);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(EXIT_FAILURE);
}
return 0;
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Bill Cunningham写道:
Bill Cunningham wrote:
我有这个代码我想清理。
#include< stdio.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
double x,y,a,b;
FILE * fp;
x = strtod(argv [1],NULL);
y = strtod(argv [2],NULL);
a = strtod(argv [ 3],NULL);
b = strtod(argv [4],NULL);
if((fp = fopen(argv [4]," a") )== NULL){
puts(fopen error);
退出(-1);
}
fprintf(fp,"%。2f \ t%。2f \ t%。2f \ t%。2f \ n",x,y,a,b);
if(fclose(fp)== EOF){
puts(fclose error);
exit(-1);
}
返回0;
}
如果是e程序运行没有参数我得到一个seg错误。如果用
4运行,没问题。如果它运行少于四个(包括argv [0])那么
程序不想运行正确。我怎么能用这个
程序说一两个argv?
I have this code I would like to clean up.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
double x, y, a, b;
FILE *fp;
x = strtod(argv[1], NULL);
y = strtod(argv[2], NULL);
a = strtod(argv[3], NULL);
b = strtod(argv[4], NULL);
if ((fp = fopen(argv[4], "a")) == NULL) {
puts("fopen error");
exit(-1);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(-1);
}
return 0;
}
If the program is run with no arguments I get a seg fault. If it is run with
4, no problem. If it is run with less than four (that includes argv[0]) then
the program doesn''t want to run right. How would I be able to use this
program with say one or two argvs ?
你的要求不完整。
如果参数少于四个,会发生什么?应该打开一个文件
吗?是否应输出错误信息?
如果没有这些细节,可能存在许多相互矛盾的解决方案。
-
Ian Collins。
Your requirements are incomplete.
What should happen if there are less than four arguments? Should a file
be opened? Should an error message be output?
Without these details, there a number of conflicting possible solutions.
--
Ian Collins.
" Richard Heathfield" < rj*@see.sig.invalidwrote in message
news:wL ********************* @ bt.com .. 。
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:wL*********************@bt.com...
#include< stdio.h>
#include< stdlib.h>
#define DEFAULT_FILE_NAME" foo.bar"
int main(int argc,char * argv [])
{
double x = 0.0,y = 0.0,a = 0.0,b = 0.0;
const char * filename = DEFAULT_FILE_NAME;
FILE * fp = NULL;
if(argc 1)
{
x = strtod(argv [1],NULL);
}
if(argc 2)
{
y = strtod(argv [2],NULL);
}
if(argc 3)
{
a = strtod(argv [3],NULL);
}
if(argc 4)
{
b = strtod(argv [4],NULL);
filename = argv [4];
}
if((fp = fopen(filename," a"))== NULL){
pu ts(fopen error);
退出(EXIT_FAILURE);
}
fprintf(fp,"%。2f \ t%.2f \ t%。2f \ t%。2f \ n",x,y,a,b);
if(fclose(fp)== EOF){
put(fclose error);
退出(EXIT_FAILURE);
}
返回0; < br $>
}
#include <stdio.h>
#include <stdlib.h>
#define DEFAULT_FILE_NAME "foo.bar"
int main(int argc, char *argv[])
{
double x = 0.0, y = 0.0, a = 0.0, b = 0.0;
const char *filename = DEFAULT_FILE_NAME;
FILE *fp = NULL;
if(argc 1)
{
x = strtod(argv[1], NULL);
}
if(argc 2)
{
y = strtod(argv[2], NULL);
}
if(argc 3)
{
a = strtod(argv[3], NULL);
}
if(argc 4)
{
b = strtod(argv[4], NULL);
filename = argv[4];
}
if ((fp = fopen(filename, "a")) == NULL) {
puts("fopen error");
exit(EXIT_FAILURE);
}
fprintf(fp, "%.2f\t%.2f\t%.2f\t%.2f\n", x, y, a, b);
if (fclose(fp) == EOF) {
puts("fclose error");
exit(EXIT_FAILURE);
}
return 0;
}
谢谢理查德我知道我总是可以依靠你。我想知道argc
及其用途。
Bill
Thanks Richard I know I can always count on you. I wondered about argc
and its uses.
Bill
这篇关于代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!