从文件重定向输入 [英] Redirecting input from file

查看:59
本文介绍了从文件重定向输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在制作一个从txt文件中获取输入的程序,做一些计算,然后将结果输出到另一个txt文件。

我编写的程序对我编译很好,但是,当我运行它时,它停止并且什么都不做。我想知道是否有什么东西显然我错过了。我的代码在下面,任何帮助将是

赞赏。


谢谢,

Dave


#include< stdio.h>

#include< stdlib.h>

#include< math.h>


int main(int argc,char * argv [])

{

/ *变量声明* /

const int arraylength = 5000;

const int errorcode = -1;

浮点数= 0;

int count = 0;

int condition = 1;

float * precipvals =(float *)NULL;

float * precip3hrs =(float *)NULL;

long * date =(long *)NULL;

FILE * fpin;

FILE * fpout;


fpin = fopen(argv [1]," r +");

fpout = fopen(argv [2]," w");


/ *数组分配* /

steadyvals =(float *)malloc(sizeof(int)* arraylength);

precip3hrs =(float *)malloc(sizeof(int )* arraylength);

date =(long *)malloc(sizeof(int)* arraylength);


/ *计算部分* /

while(条件!= EOF){

condition = fscanf(fpin,"%l ,%f",& date [count],& sedivals [count]);

if(feof(fpin)){

fclose(fpin);

返回-3;

}

if(count == 1){

precip3hrs [count] = sedivals [count];

}

if(count == 2){

precip3hrs [count] = degravals [count] + precip3hrs [count -1];

}

如果(计数2){

precip3hrs [count] =沉积[计数] +沉淀[count-1] ] +

降水[count-2];

}

}


/ *输出部分* /

for(count = 1; count< = arraylength;计数++){

if(count == 1){

printf("一小时降水量:%2.2f三小时降水量%2.2f

\ n",sedivals [count],precip3hrs [count]);

}

if(count == 2){

printf(一小时降水:%2.2f三小时降水%2.2f

\ n,降水量[count],degra3hrs [count]);

}

if(count 2){

printf("一小时降水量:%2.2f三小时降水量%2.2f

\ n,沉淀[count],precip3hrs [count]);

}

}


/ *写入文件* /

for(count = 1; count< = arraylength; count ++){

if(count == 1){

fprintf( fpout,一小时降水:%2.2f三小时

降水%2.2f\ n,降水量[count],degra3hrs [count]);

}

if(count == 2){

fprintf(fpout,"一小时降水量:%2.2f三小时

preci pitation%2.2f \ n,沉淀[count],precip3hrs [count]);

}

if(count 2){

fprintf(fpout,一小时降水:%2.2f三小时

降水%2.2f\ n,降水量[count],degra3hrs [count]);

}

}

fclose(fpin);

fclose(fpout);


/ *释放* /

免费(日期);

date =(long *)NULL;

免费(降水量);

Canyvals =(float *)NULL;

free(precip3hrs);

precip3hrs =(float *)NULL;

返回0;

}

Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I''ve written compiles fine for me, however, when I
run it, it stalls and does nothing. I''m wondering if there''s something
obvious that I''m missing. My code is below and any help would be
appreciated.

Thanks,
Dave

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");

/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);

/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}

/* Output section */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}

推荐答案

dm ****** @ cox.net 写于06/06/28 11:30,:
dm******@cox.net wrote On 06/28/07 11:30,:

大家好,我正在研究一个从txt文件中获取输入的程序,做一些计算,然后将结果输出到另一个txt文件。

该程序我写的编译很好但是,当我运行它时,它会停滞不动。我想知道是否有什么东西显然我错过了。我的代码在下面,任何帮助将是

赞赏。
Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I''ve written compiles fine for me, however, when I
run it, it stalls and does nothing. I''m wondering if there''s something
obvious that I''m missing. My code is below and any help would be
appreciated.



有几个问题,也许比我在快速扫描中发现的b $ b更多。我在下面提到了一些:

There are several problems, and perhaps more than I''ve
spotted in a quick scan. I''ve noted a few of them below:


#include< stdio.h>

#include< stdlib.h> ;

#include< math.h>

int main(int argc,char * argv [])

{

/ *变量声明* /

const int arraylength = 5000;

const int errorcode = -1;

float sum = 0;

int count = 0;

int condition = 1;

float * precipvals =(float *)NULL;

float * precip3hrs =(float *)NULL;

long * date =(long *)NULL;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;



演员阵容无害,但不必要。

The casts are harmless, but unnecessary.


FILE * fpin;

FILE * fpout;


fpin = fopen(argv [1]," r +");
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");



为什么r +而不只是r,因为你所做的只是从文件中读取
?另外,你应该检查fopen()调用返回的值

,看看fopen()是否失败。

Why "r+" instead of just "r", since all you do is
read from the file? Also, you should check the values
returned by both fopen() calls to see if fopen() failed.


fpout = fopen( argv [2]," w");


/ *数组分配* /

Canyvals =(float *)malloc(sizeof(int)* arraylength);

precip3hrs =(float *)malloc(sizeof(int)* arraylength);

date =(long *)malloc(sizeof(int)* arraylength) ;
fpout = fopen(argv[2],"w");

/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);



更多不必要但无害的演员。更糟糕的问题

是你没有检查任何malloc()调用

失败。最糟糕的是,你需要空间来支付这么多的赌注,但是你实际要去的东西是
存储的是花车和多头! br />

More unnecessary but harmless casts. A worse problem
is that you don''t check any of the malloc() calls for
failure. Worst of all is that you request space for
so-and-so many ints, but the things you''re actually going
to store are floats and longs!


>

/ *计算部分* /

while(条件!= EOF){

condition = fscanf(fpin,"%l,%f",& date [count],& sedivals [count]);

if(feof(fpin)) {

fclose(fpin);

返回-3;
>
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);
if(feof(fpin)) {
fclose(fpin);
return -3;



这是一个非常奇怪的退出价值。它可能意味着你的系统上有一些东西,但是如果它确实意味着远远不是通用的b $ b。唯一的便携式退出值为EXIT_SUCCESS

和零(均表示成功)和EXIT_FAILURE。

That''s a pretty weird exit value. It may mean something
on your system, but if it does the meaning is far from
universal. The only "portable" exit values are EXIT_SUCCESS
and zero (both meaning "success") and EXIT_FAILURE.


}

if(count == 1){

precip3hrs [count] = degravals [count];

}

if(count == 2) {

precip3hrs [count] = degravals [count] + precip3hrs [count-1];

}

if(count 2){

precip3hrs [count] =沉积[计数] +沉淀[count-1] +

沉降[count-2];

}
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}



由于计数初始化为零并且没有任何改变其值的
,这个循环只是读取和读取并读取

并且什么都不做。另外,如果fscanf()曾经遇到过某些东西

它无法解释,那么它将返回它可以转换的物品数量(零或者一,并留下难以理解的

垃圾还没读。在下次通话时,它仍然无法转换垃圾,因此它将返回零并留下垃圾

未读取。在接下来的电话中它仍然无法...


结果:如果输入中的任何地方出现任何问题,

此循环将从未完成。如果输入数据是完全格式化的,那么这个循环将一直读取文件

并且基本上什么都不做。当(if)它到达

结束输入时,feof(pin)将返回true并且程序将退出

Since count was initialized to zero and there''s nothing
to change its value, this loop just reads and reads and reads
and does nothing. Also, if fscanf() ever runs across something
it can''t interpret, it''ll return the number of items it was
able to convert (zero or one) and leave the unintelligible
junk still un-read. On the next call it will still be unable
to convert the junk, so it will return zero and leave the junk
un-read. On the next call it will still be unable ...

The upshot: If there''s anything wrong anywhere in the input,
this loop will never finish. If the input data is perfectly
formatted, this loop will read all the way through the file
and do essentially nothing. When (if) it reaches the end of
the input, feof(pin) will return true and the program will
exit.


}
}



据我所知,该程序没有办法

到此为止。但是如果它确实......

As far as I can see, there''s no way for the program to
arrive at this point. But if it did ...


/ *输出部分* /

for(count = 1; count< = arraylength; count ++ ){
/* Output section */
for(count = 1; count <= arraylength; count++) {



在这个循环的最后一次旅行中,计数将等于

arraylength,你会尝试使用[arraylength]元素
你的数组
。由于你只通过[arraylength-1]为[0]

分配了空间(如果是这样;见上文),那将是

a问题。


此外,唯一存储了

的数组元素是日期和渐变的[0]元素。

没有任何东西存储在这两个数组中编号较高的元素中,也没有存储在任何沉降元素中。


最后,假设你修复了输入循环因此它实际上是
做了一些有用的事情,如果输入有3000

行而不是5000行会怎么样? (或者更糟糕的是:如果它有6000怎么办?)

你真的想要遍历整个数组,或者只是通过与输入对应的数组元素来获得
吗? br />

On the final trip through this loop, count will equal
arraylength and you''ll try to use the [arraylength] elements
of your arrays. Since you''ve only allocated space for [0]
through [arraylength-1] (if that; see above), that''ll be
a problem.

Also, the only array elements that have ever had anything
stored in them are the [0] elements of date and precipvals.
Nothing has ever been stored in the higher-numbered elements
of those two arrays, nor in any element of precip3hrs.

Finally, assuming you fix up the input loop so it actually
does something useful, what happens if the input has 3000
lines instead of 5000? (Or worse: What if it has 6000?)
Do you really want to loop through the entire array, or just
through the array elements that correspond to inputs?


if(count == 1){

printf("一小时降水量:%2.2f三小时降水量%2.2f

\ n",sedivals [count],precip3hrs [count]);

}

if(count == 2){

printf(一小时降水量:%2.2f三小时降水量%2.2f

\ n,降水量[count],degra3hrs [count]);

}

if(count 2){

printf("一小时降水量:%2.2f三小时降水量%2.2f

\ n",sedivals [count],precip3hrs [count]);

}

}


/ *写入文件* /

for(count = 1; count< = arraylength; count ++){
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) {



与之前相同的评论循环。

Same remarks as for the previous loop.


if(count == 1){

fprintf(fpout,一小时降水量:%2.2f三小时

降水% 2.2f\ n,沉淀[count],precip3hrs [count]);

}

if(count == 2){

fprintf(fpout,一小时降水:%2.2f三小时

降水%2.2f\ n,降水量[count],degra3hrs [count]);

}

if(count 2){

fprintf(fpout,"一小时降水量:%2.2f三小时

降水% 2.2f\ n,沉淀[count],precip3hrs [count]);

}

}

fclose(fpin);

fclose(fpout);
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);



信不信由你,fclose()可能会失败。

第一个的失败可能不会对你的程序造成任何损害,但如果第二个失败则很可能意味着它的某些部分

输出文件没有写入。你想宣布

成功!如果发生这种情况?

Believe it or not, fclose() can fail. Failure of the
first one probably doesn''t hurt your program any, but if
the second fails it very likely means that some part of the
output file didn''t get written. Do you want to proclaim
"Success!" if that happens?


/ *释放* /

免费(日期);

date =(long *)NULL;

免费(降水量);

Canyvals =(float *)NULL;

免费(precip3hrs);

precip3hrs =(float *)NULL;
/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;



更多无用的演员表。 (无用的任务,恕我直言。)

More useless casts. (Useless assignments, too, IMHO.)


返回0;

}
return 0;
}



-
Er*********@sun.com


嗨Dave,下面有一些提示:
Hi Dave, some hints below :

Dave


#include< stdio.h>

#include< stdlib.h>

#include< math.h>


int main(int argc,char * argv [])

{

/ *变量声明* /

const int arraylength = 5000;

const int errorcode = -1;

浮点数= 0;

int count = 0;

int condition = 1;

float * precipvals =(float *)NULL;

float * precip3hrs =(float *)NULL;

long * date =(long *)NULL;

FILE * fpin;

FILE * fpout;


fpin = fopen( argv [1],r +);

fpout = fopen(argv [2]," w");


/ *氩ray Allocation * /

precipvals =(float *)malloc(sizeof(int)* arraylength);

precip3hrs =(float *)malloc(sizeof(int)* arraylength );

date =(long *)malloc(sizeof(int)* arraylength);
Dave

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
const int errorcode = -1;
float sum = 0;
int count = 0;
int condition = 1;
float* precipvals = (float*)NULL;
float* precip3hrs = (float*)NULL;
long* date = (long*)NULL;
FILE *fpin;
FILE *fpout;

fpin = fopen(argv[1],"r+");
fpout = fopen(argv[2],"w");

/* Array Allocation */
precipvals = (float*)malloc(sizeof(int) * arraylength);
precip3hrs = (float*)malloc(sizeof(int) * arraylength);
date = (long*)malloc(sizeof(int) * arraylength);



检查malloc是否返回NULL指针。

check if malloc returned a NULL pointer.


>

/ *计算部分* /

while(条件!= EOF){

condition = fscanf(fpin,"%l,%f",& date [count ],& sedivals [count]);
>
/* Calculation Section */
while(condition != EOF) {
condition = fscanf(fpin,"%l,%f", &date[count], &precipvals[count]);



你能告诉我们这个档案吗?似乎计数永远不会增加

并且总是等于1?

Could you show us the file ? It seems that count is never incremented
and always equal 1 ?


if(feof(fpin)){

fclose(fpin);

返回-3;

}

if(count == 1){

precip3hrs [count] =沉淀[count];

}

if(count == 2){

precip3hrs [count] =沉降[count] + precip3hrs [count-1];

}

if(count 2){

precip3hrs [count] = degravals [计数] +沉淀[count-1] +

沉降[count-2];

}

}

/ *输出部分* /

for(count = 1; count< = arraylength; count ++){

if(count == 1){

printf(一小时降水量:%2.2f三小时降水量%2.2f

\ n,降水量[count],precip3hrs [count]);

}
if(feof(fpin)) {
fclose(fpin);
return -3;
}
if(count == 1) {
precip3hrs[count] = precipvals[count];
}
if(count == 2) {
precip3hrs[count] = precipvals[count] + precip3hrs[count-1];
}
if(count 2) {
precip3hrs[count] = precipvals[count] + precipvals[count-1] +
precipvals[count-2];
}
}

/* Output section */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}



如果count == arraylength,那么你会溢出。您可以使用索引[0到arrayval-1]在

值内写入。

If count == arraylength, then you do an overflow. You can write inside
precipvals using index [0 to arrayval-1].


if(count == 2){

printf(一小时降水量:%2.2f三小时降水量%2.2f

\ n,降水量[count],degra3hrs [count]);

}

if(count 2){

printf("一小时降水量:%2.2f三小时降水量%2.2f

\ n",sedivals [count],precip3hrs [count]);

}

}


/ *写入文件* /

for(count = 1; count< = arraylength; count ++){

if(count == 1){

fprintf(fpout,一小时降水:%2.2f三小时

降水%2.2f\ n,降水量[count],degra3hrs [count]);

}
if(count == 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
printf("One hour precipitation: %2.2f Three hour precipitation %2.2f
\n",precipvals[count],precip3hrs[count]);
}
}

/* Write to file */
for(count = 1; count <= arraylength; count++) {
if(count == 1) {
fprintf(fpout, "One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}



关于溢出的相同评论。

same comment about overflows.


if(count == 2){

fprintf(fpout,一小时降水:%2.2f三小时

沉淀%2.2f\ n,沉淀[count],precip3hrs [count]);

}

if(count 2){

fprintf(fpout,一小时降水:%2.2f三小时

降水%2.2f\ n,降水量[count],precip3hrs [count]);

}

}

fclose(fpin);

fclose(fpout);


/ *释放* /

免费(日期);

date =(long *)NULL;

免费(降水量);

Canyvals =(float *)NULL;

free(precip3hrs);

precip3hrs =(float *)NULL;

返回0;

}
if(count == 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
if(count 2) {
fprintf(fpout,"One hour precipitation: %2.2f Three hour
precipitation %2.2f\n",precipvals[count],precip3hrs[count]);
}
}
fclose(fpin);
fclose(fpout);

/* Deallocation */
free(date);
date = (long*)NULL;
free(precipvals);
precipvals = (float*)NULL;
free(precip3hrs);
precip3hrs = (float*)NULL;
return 0;
}



dm ****** @ cox.net 说:

大家好,我正在制作一个节目从txt文件中获取输入,进行一些计算,然后将结果输出到另一个txt文件。

我写的程序编译好我但是,当我
运行它,它停止并且什么都不做。我想知道是否有什么东西显然我错过了。我的代码在下面,任何帮助将是

赞赏。
Hi All, I am working on a program to take input from a txt file, do
some calculations, and then output the results to another txt file.
The program that I''ve written compiles fine for me, however, when I
run it, it stalls and does nothing. I''m wondering if there''s something
obvious that I''m missing. My code is below and any help would be
appreciated.



我做了一些明显的修正 - 删除了所有演员,添加了一点

的错误检查,修复了几个破坏循环边界...


但是我无法测试,因为我没有你拥有的数据。


所以这是半修正的代码。我不是说它解决了你的问题,

但是你可能会发现它至少在运行时打印出一个投诉,

在这种情况下它会对你有用知道它打印的投诉。


#include< stdio.h>

#include< stdlib.h>

#include< math.h>


int main(int argc,

char * argv [])

{

/ *变量声明* /

const int arraylength = 5000;

int count = 0;

int条件= 1;

float * precipvals = NULL;

float * precip3hrs = NULL;

long * date = NULL;

FILE * fpin;

FILE * fpout;


if(argc 2)

{

fpin = fopen(argv [1]," r +");

if(fpin!= NULL)

{

fpout = fopen(argv [2]," w");

if(fpout!= NULL)

{

/ *阵列分配* /

悬崖= malloc(arraylength *

尺寸*沉淀物);

如果(沉淀!=空)

{

precip3hrs = malloc(arraylength *

sizeof * precip3hrs);

if(precip3hrs!= NULL)

{

date = malloc(arraylength *

sizeof * date);

if(date!= NULL)

{

/ *计算部分* /

while(条件!= EOF)

{

条件=

fscanf(fpin,

" ;%ld,%f",

& date [count],

& sedivals [count]);

if(feof) (fpin))

{

fclose(fpin);

fprintf(stderr,

" Last datum读取,程序现在关闭。\ n");

返回EXIT_FAILURE;

}

if(count == 1)

{

precip3hrs [count] = degravals [count];

}

else if(count == 2)< br $>
{

precip3hrs [count] =

shavvals [count] + precip3hrs [count - 1];

}

else if(count 2)

{

precip3hrs [count] =

降水量[数量] +降水量[数量 - 1] +

降水量[count - 2];

}

}


/ *输出部分* /

for(count = 1;计数< arraylength;计数++)

{

if(count == 1)

{

printf

(一小时降水量:%2.2f

三小时降水量%2.2f\ n,

降水量[count],
precip3hrs [count]);

}

否则if(count == 2)

{

printf

(一小时降水量:%2.2f

三小时降水量%2.2f\ n,

growvals [count],

precip3hrs [count]);

}

else if(count 2)

{

printf

(一小时降水量:%2.2f

三小时降水量%2.2f\ n ;,

降水[count],

precip3hrs [count]);

}

}


/ *写入文件* /

for(count = 1; count< arraylength; count ++)

{

if(count == 1)

{

fprintf(fpout,

"一小时降水量:%2.2f"

"三小时降水%2.2f \ n,

降水[数量],

precip3hrs [count]);

}

if(count == 2)

{

fprintf(fpout,

"一小时降水量:%2.2f" ;

三小时降水%2.2f\ n,

降水[count],

precip3hrs [count]);

}

if(count 2)

{

fprintf(fpout,

一小时降水:%2.2f

三小时降水%2.2f\ n,

降水[count],

precip3hrs [count]);

}

}

免费(日期);

}

其他

{

fprintf(stderr,分配失败:date\\\
);

} < br $>
免费(precip3hrs);

}

其他

{

fprintf(stderr ,分配失败:degra3hrs \ n;)

}

免费(降水量);

}

else

{

fprintf(stderr,Allocation failure:precipvals\\\
);

}

fclose(fpout);

}

else

{

fprintf(stderr," ;不能打开%s进行写作。\ n",argv [2]);

}

fclose(fpin);

}

其他

{

fprintf(stderr,不能打开%s阅读\ n,argv [1]);

}

}

其他

{

fprintf (stderr,需要更多args.\ n);

}


返回0;

}



-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件: -万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

I''ve made some obvious corrections - removed all the casts, added a bit
of error checking, fixed a couple of broken loop bounds...

But I can''t test because I don''t have the data that you have.

So here''s the semi-corrected code. I''m not saying it fixes your problem,
but you might find that it at least prints out a complaint at runtime,
in which case it would be useful to know which complaint it prints.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc,
char *argv[])
{
/* Variable Declarations */
const int arraylength = 5000;
int count = 0;
int condition = 1;
float *precipvals = NULL;
float *precip3hrs = NULL;
long *date = NULL;
FILE *fpin;
FILE *fpout;

if(argc 2)
{
fpin = fopen(argv[1], "r+");
if(fpin != NULL)
{
fpout = fopen(argv[2], "w");
if(fpout != NULL)
{
/* Array Allocation */
precipvals = malloc(arraylength *
sizeof *precipvals);
if(precipvals != NULL)
{
precip3hrs = malloc(arraylength *
sizeof *precip3hrs);
if(precip3hrs != NULL)
{
date = malloc(arraylength *
sizeof *date);
if(date != NULL)
{
/* Calculation Section */
while(condition != EOF)
{
condition =
fscanf(fpin,
"%ld,%f",
&date[count],
&precipvals[count]);
if(feof(fpin))
{
fclose(fpin);
fprintf(stderr,
"Last datum read, program closing now.\n");
return EXIT_FAILURE;
}
if(count == 1)
{
precip3hrs[count] = precipvals[count];
}
else if(count == 2)
{
precip3hrs[count] =
precipvals[count] + precip3hrs[count - 1];
}
else if(count 2)
{
precip3hrs[count] =
precipvals[count] + precipvals[count - 1] +
precipvals[count - 2];
}
}

/* Output section */
for(count = 1; count < arraylength; count++)
{
if(count == 1)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
else if(count == 2)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
else if(count 2)
{
printf
("One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
}

/* Write to file */
for(count = 1; count < arraylength; count++)
{
if(count == 1)
{
fprintf(fpout,
"One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
if(count == 2)
{
fprintf(fpout,
"One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
if(count 2)
{
fprintf(fpout,
"One hour precipitation: %2.2f"
" Three hour precipitation %2.2f\n",
precipvals[count],
precip3hrs[count]);
}
}
free(date);
}
else
{
fprintf(stderr, "Allocation failure: date\n");
}
free(precip3hrs);
}
else
{
fprintf(stderr, "Allocation failure: precip3hrs\n");
}
free(precipvals);
}
else
{
fprintf(stderr, "Allocation failure: precipvals\n");
}
fclose(fpout);
}
else
{
fprintf(stderr, "Can''t open %s for writing.\n", argv[2]);
}
fclose(fpin);
}
else
{
fprintf(stderr, "Can''t open %s for reading\n", argv[1]);
}
}
else
{
fprintf(stderr, "Need more args.\n");
}

return 0;
}


--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于从文件重定向输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆