title: Seg faulting from loading data from a CSV file
body: <p>I am trying to write a program that takes historical s and p 500 stock data (1999-2013) from a csv file and print the contents to the screen. The csv files have the format of [yyyymmdd,0,openprice,closeprice,low,high,volume] for each line that I am taking in. For instance stock a with data [19991123,0,42.2029,45.5656,39.0001,50.2143, 6753909] means that on November 23 1999 stock a had an opening price of 42.2029, closing price of 45.5656 and so on. My program takes in the information in my while loop and it is able to do it up to a point where the data is shown as zero and the price is shown as 2. Mag is the char array that everything is being based off of. ypg and chr take the csv data line by line. ypg and chr then subsequently modify each line until all you have left are the opening price and date respectively.<a href="https://quantquote.com/historical-stock-data" rel="nofollow noreferrer"> 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</a>. </p>
  
  <p>Hopefully I am just doing something stupid thats really solvable. Any help is greatly appreciated.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  //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(&amp;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(&amp;Stream, &amp;stream, dest);
  
  return 0;
  }
  </code></pre>
  
  <p><strong>I expect to get results similar to this and repeating until I reach the end of my file.</strong></p>
  
  <pre><code>The value of Date is 19991118                                
  The value of Opening Price is: 42.207600
  </code></pre>
  
  <p><strong>When I straight up run the program. My end result ends up being this. The exception being that the first four lines below stretch to the limits of my linux terminal.</strong></p>
  
  <pre><code>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.
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/86d1I.png" rel="nofollow noreferrer">Here is a screenshot reference.</a></p>
  
  <p><strong>This is what happens when I do a debugging session with breakpoints set at LOAD and line 83 (Notice how the numbers start off fine but then get screwed up):</strong></p>
  
  <pre><code>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.
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/VZQio.png" rel="nofollow noreferrer">Here is another screenshot for sake of reference.</a></p>