我需要指定一個包含300000個數據的字符數組,這個數組是從包含以列格式(其龐大的數據文件)排列的200,000個數字的Numbers.dat文件中獲取的。操作是從該文件中提取數據並將其存儲在300000的數組中,以便將這些300000數字再次存儲在不同的文件中。此操作針對兩個文件執行,因此這兩個文件的子集的形式爲錯誤數組大小
-0.98765
-0.124567
etc 但我得到兩個錯誤:首先是語法錯誤,說數組大小太長,另一個是邏輯錯誤。如何解決這個問題。該代碼是如下由槍手在How to read blocks of numbers from a text file in C提供,但不用於這種情況下
#include <stdio.h>
#include<stdlib.h>
# include <conio.h>
# include <dos.h>
# include <math.h>
void main()
{ FILE *fpt1,*fpt2,*fpt;
fp=fopen("numbers.dat","r");
fpt1=fopen("subset1.dat","w");
fpt2=fopen("subset2.dat","w");
int index=0;
char anum[300000]; //this is the reason for the first syntactic error :Array size too large
// since we are not calculating, we can store numbers as string
while(fscanf(fp,"%s",anum) == 1)
{
if(index==0)
{
// select proper output file based on index.
fprintf(fpt1,"%s",anum);
index++; }
if(index ==300000)
{
fprintf(fpt2,"%s",anum);
index++; }
}
fclose(fp);
fclose(fpt1);
fclose(fpt2);
}
邏輯錯誤是,只有一個號碼被寫入在文件SUBSET1和SUBSET2甚至當我減小尺寸至300塊時的工作數據的。
請顯示您的代碼? – 2011-03-11 08:34:58
哪個來源?哪個編譯器?哪個命令行?哪個錯誤信息?如何以智能的方式提出問題:http://www.catb.org/~esr/faqs/smart-questions.html – DevSolar 2011-03-11 08:36:03
@To all:我有TURBO C++ FOR WINDOWS 4.5。 – SKM 2011-03-11 08:50:12