2017-12-09 4 views
-1

csvファイルからヒストリカルsとp 500のストックデータ(1999-2013)を取得し、その内容を画面に出力するプログラムを作成しようとしています。 csvファイルは、私が取り込んでいる各行に対して[yyyymmdd、0、openprice、closeprice、low、high、volume]のフォーマットを持っています。たとえば、データa [19991123,0,42.2029,45.5656,39.0001,50.2143、 6753909]は、1999年11月23日の株価が42.2029で、終値が45.5656であったことを意味する。私のプログラムは私のwhileループの情報を取り込み、データがゼロとして表示され、価格が2として表示されるまでそれを行うことができます。Magは、すべてが基になっているchar配列です。 ypgとchrはcsvデータを1行ずつ取ります。 ypgとchrはその後、残りのすべてが開始時の価格と日付になるまで、各行を変更します。 Here is a link to a webpage that will give you a zip file containing the CSV's of all the stocks I am using. Just click Free Data and then click DownloadCSVファイルからデータをロードすることによってセグフラムが発生する

うまくいけば、私はちょうど本当に解決できる何か愚かなことをやっています。どんな助けでも大歓迎です。

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 
#include <unistd.h> 

//This gives us the length of the string for the file name which is vary 
//useful 
//for calling the file and assigning it memory. 
int mystrlen(const char *(*s)) 
{ 
int i =0; 
while (*s++) i++; 
return i; 
} 


//This is where the linked list is basically being created. 
void LOAD(FILE *(*Stream), FILE *(*stream), const char *stock) 
{ 

//Opening and rewinding file. 
int strlength = mystrlen(&stock); 
char src[37+strlength]; 
strcpy(src,"./quantquote_daily_sp500_83986/daily/"); 
strcat(src,stock); 
*stream = fopen(src, "r"); 
*Stream = fopen(src,"r"); 
rewind(*stream); 

if(*stream == NULL) 
{ 
printf("Error, invalid stock name. restarting program\n"); 
main(); 
} 

//while loop to print off the data to the screen. 
printf("Entering the while loop\n"); 
while(1) 
{ 
if((feof(*stream))) 
{ 
break; 
} 
char * Mag[55] = { NULL }; 

char * chr = (char *)malloc(sizeof(Mag)); 
char * ypg = (char *)malloc(sizeof(Mag)); 

fgets(chr, sizeof(Mag), *stream); 
fgets(ypg, sizeof(Mag), *Stream); 

char DATE[9]; 
char OPENPRICE[6]; 

//these create the variables necessary for the datalist. 
char * date = (char *)malloc(sizeof(chr)); 
date = chr; 

char * price = (char *)malloc(sizeof(ypg)); 
price = ypg; 

strtok(date,",0,"); 

price = strtok(NULL,","); 
price = strtok(NULL,","); 
strcpy(DATE,date); 
strcpy(OPENPRICE,price); 
int DAte = NULL; 
DAte = atoi(DATE); 
double openprice = 0; 
openprice = atof(OPENPRICE); 

//these two methods are supposed to populate datalist to the screen but 
//I keep getting errors. 

int * Date = (int *)malloc(9); 
*Date = (int)DAte; 
printf("The value of Date is %d\n", *Date); 

double * OpenPrice = (double *)malloc(6); 
*OpenPrice = openprice; 
printf("The value of Opening Price is: %lf\n", *OpenPrice); 


free(Date); 
free(OpenPrice); 

free(chr); 
free(ypg); 
} 
fclose(*stream); 
return; 
} 

//main function for program. 
int main(int argc, char **argv) 
{ 
printf("Welcome to Stock Simulator.\n"); 

char stock[50]; 
FILE *stream; 
FILE *Stream; 
int size; 
char src[50], dest[50]; 
char *namen = (char *)malloc(sizeof(char)); 

printf("\n"); 
printf("What is the name of the stock that you would like to analyze 
(Enter the stock ticker below):\n"); 
scanf("%s",stock); 

strcpy(dest, "table_"); 
strcat(dest, stock); 

printf("%s",dest); 
printf("\n"); 

strcpy(src, ".csv"); 
strcat(dest, src); 
printf("%s",dest); 
printf("\n"); 

LOAD(&Stream, &stream, dest); 

return 0; 
} 

私は、ファイルの最後に到達するまで、これに類似し、繰り返しの結果を得ることを期待しています。

The value of Date is 19991118         
The value of Opening Price is: 42.207600 

私はまっすぐに、プログラムを実行します。私の最終的な結果はこれで終わる。例外は、私のLinux端末の限界まで伸びる最初の4行が例外であることです。

The value of Date is 2.       
The value of Opening Price is: 0.000000.     
The value of Date is 2.      
The value of Opening Price is: 0.000000. 

Program received signal SIGSEGV, Segmentation fault. 
__strcpy_sse2_unaligned() at ../sysdeps/x86_64/multiarch/strcpy-   sse2-unaligned.S:296            
296 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory. 

Here is a screenshot reference.

は、これは私が(数字は罰金始めるが、その後めちゃくちゃを取得する方法に注目してください)LOADとライン83に設定されたブレークポイントでデバッグセッションを実行するときに何が起こるかです:

The value of Date is 19991126       
The value of Opening Price is: 37.921900 




Breakpoint 2, LOAD (Stream=0x7fffffffdf30, stream=0x7fffffffdf28,   stock=0x7fffffffdfc0 "table_a.csv") at extracredit.c:83      
83  free(OpenPrice);        
(gdb) continue       
Continuing.          
The value of Date is 19991129       
The value of Opening Price is: 38.033200 



Breakpoint 2, LOAD (Stream=0x7fffffffdf30, stream=0x7fffffffdf28,   stock=0x7fffffffdfc0 "table_a.csv") at extracredit.c:83    
83  free(OpenPrice);             
(gdb) continue                
Continuing.                
The value of Date is 1999113           
The value of Opening Price is: 38.960900         

Breakpoint 2, LOAD (Stream=0x7fffffffdf30, stream=0x7fffffffdf28,   stock=0x7fffffffdfc0 "table_a.csv") at extracredit.c:83     
83  free(OpenPrice);              
(gdb) continue               
Continuing.             
The value of Date is 199912.           
The value of Opening Price is: 0.000000. 

Here is another screenshot for sake of reference.

+2

これは非常に広範である不要なコードのトンが含まれており、実際の文が含まれていない必要があります(デバッガの出力など、画像へのリンクはテキストとほとんど変わりません)。ここで[質問]の質問をお読みください。 –

+0

これは私がこのサイトで尋ねた最初の質問です。これにつきましては申し訳ございません。 – Jack

+0

問題ありません。このサイトは、使用するほとんどのサイトとは少し異なります。おそらく、エラーメッセージやデバッガの出力を含めたり、関連するコードだけを投稿したりすることで、良い質問にすることができます。 –

答えて

1

問題は、あなたのLOADである()wherあなたがしたい場合は、2番目の真のランタイムメモリ割り当ての使用のmalloc()

int strlength = mystrlen(&stock); 
char src[37+strlength]; 

間違っあるの大きさを与えるための変数を取る電子。 正しい方法は以下の通りです:

char *src; 
src = malloc (37 + strlength) 
0

文字列のポインタへ ポインタが代わりに文字列へのポインタ逆参照されているため、あなたのmystrlen機能が動作しません。

int mystrlen(const char *(*s)) 
{ 
    int i =0; 
    while (*s++) i++; // s contains address of pointer to string, 
        // s doesn't point to characters 
    return i; 
} 

逆参照文字の前に、あなたは、文字列へのポインタデリファレンス、あなたの関数の 新バージョンでは、

int mystrlen(const char *(*s)) 
{ 
    int i =0; 
    const char* ptrToString = *s; 
    while (*ptrToString++) i++; 
    return i; 
} 
関連する問題