加载来自CSV文件

时间:2017-12-09 20:18:20

标签: c csv terminal

我正在尝试编写一个程序,该程序从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数据。然后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 Download

希望我只做一些可以解决的愚蠢行为。非常感谢任何帮助。

#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终端的限制。

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.

1 个答案:

答案 0 :(得分:1)

问题在于你的LOAD(),如果你想要运行时内存分配使用malloc()

,你可以使用一个变量给出第二个值的大小。
int strlength = mystrlen(&stock);
char src[37+strlength];

错误。 正确的方法如下:

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