Passing Arrays in Structure

Can't read file using structure reference

Page 1 of 1

0 Replies - 778 Views - Last Post: 26 October 2009 - 09:10 AM Rate Topic: -----

#1 coxjacob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-July 09

Passing Arrays in Structure

Posted 26 October 2009 - 09:10 AM

I have written a C code that is supposed to read a list of .csv filenames from a txt file. As it reads these file names, they are stored as strings in stock_list[count].stockfile. The program is then supposed to open these files by passing the structure array value to another function getStockData() where the function extracts data from them like adj price, date, etc. The problem I am having is when I attempt to pass the stock_list[0].filename to this function. In my mind, this is just a character string; however, it fails to open the file. The function does work if I enter "aa.csv" into the function, but not when I place the structure value. I've also checked within the getStockData() when I pass stock_list[0].stockfile usning a print statement, and the print statement will display aa.csv, so I'm not sure why this doesn't work to open the file. Any pointers is much appreciated...

My Code Follows:

 * stockanalyzer.c
 *  Created on: Oct 24, 2009
 *	  Author: Jacob


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

#define MAX 100 /* Number of Stocks */
#define X_SIZE 40 /* size of array in X direction */
#define Y_SIZE 60 /* size of array in Y direction */
* We use X_SIZE/8 because we pack 8 bits per byte
char graphics[X_SIZE / 8][Y_SIZE]; /* the graphics data */
#define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8))

struct stock {
	char stockfile[7];
	char ticker[9];
	char date[15];
	float adjPrice[156];
	float adjReturn[156];
	int month[156];
	int bin[]; /* dependent on return, stocks will be placed in bins for Histogram */
struct stock stock_list[2];

int readStockList();
int getStockData(char filee[7]);
//int getStockData(struct stock filee);
int graph(void);
int fclose(FILE* file);

int main ()
	int count = readStockList();
		printf("This is a test of global. \n");
	printf("Stock Files Found are: \n");
	int i;
	//char a[9];
	for (i=0;i<=count;i++){
		printf("%s \n", stock_list[i].stockfile);
	/* want to pass stock_list[0].stockfile to my getStock() function but can't */
	/* I essentially want to pass the value aa.csv shown below, but as an array like above */
	return 0;

/* This function takes file names from stocklist.txt and loads them into the array
 * structure stockfile
int readStockList()
	FILE *file_ptr = fopen("stocklist.txt","rt");
	char fileName[9];
	int count = 0;
	int nameLength;

	if ( file_ptr == NULL )
		puts ( "cannot open file" );
	while(fgets(fileName, 10, file_ptr))
		nameLength = strlen(fileName);
		int i;
		for (i=0; i<nameLength; i++){
			stock_list[count].stockfile[i] = fileName[i];
			stock_list[count].stockfile[i+1] = '\0';
			printf("%s\n", fileName);
		printf("File Name is: %s \t \n", fileName);
		printf("File Name Loaded is: %s \t\t\n", stock_list[count].stockfile);
		printf("File Name Length =  %i\t\t\n", nameLength);
		printf("File Number is: %i\t\n", count);
		count += 1;
	return count;

int getStockData(char filee[7])
//int getStockData(struct stock )
	printf(" Filee is : %s\n", filee);
	FILE *file_ptr = fopen(filee,"r");
	//FILE *file_ptr = fopen(argv[1],"rt");
	char lineBuffer[50];
	char date[10];
	float open, high, low, close, volume, adjclose;
	int count = 0;
	if (file_ptr==0)
		   printf("Cannot open input file\n");
	printf("  DATE \t\t OPEN\t\t CLOSE\t\t ADJ ClOSE\n");
	while (fgets(lineBuffer, 50, file_ptr))
		if (count==20) break;
		int length = strlen(lineBuffer);
		int linecount;
		for (linecount=0; linecount<length; linecount++) //strip commas from csv
			if (lineBuffer[linecount] == ',')
				lineBuffer[linecount] = ' ';

		if (count>0)	//skip first line of text
		{			   //get values
			//int scan_count =
			sscanf(lineBuffer,"%s %f %f %f %f %f %f",
			&date, &open, &high, &low, &close, &volume, &adjclose);
			printf("%s\t %f\t %f\t %f\n",
					date, open, close, adjclose);

	return count;

Is This A Good Question/Topic? 0
  • +

Page 1 of 1