I'm tired of looking at this code and trying to figure out why AllocateFile doesn't work. Please let me know if you guys see anything wrong with my code. I have two problems.
1) in GetFile if a newline is entered as the first input, code crashes. Let me know what i'm not seeing please.
2)Allocatefile doesn't seem to even be filling up the buffer because i put a test statement in there and nothing prints out.
Code:
/*csv dedupper, this program goes through a a csv text file, allocates contents
to a struct, and provides sevreal functions to work on that struct and then
prints the output to a new text file. functions inlude: FindDupes, Timer etc...*/
#include<stdio.h>/*for file i/o*/
#include<string.h>/*for strchr and strncpy*/
#include <errno.h>
#define MAX 150
#define STARTOVER 0
typedef struct CSV{
char field1[MAX],field2[MAX],field3[MAX],field4[MAX],field5[MAX];
char field6[MAX],field7[MAX],field8[MAX],field9[MAX],field10[MAX];
struct CSV *left, *right;/*for linked list later and dynamic allocation*/
}RECORD;
FILE *GetFile(void);/*opens file for reading and work and returns its addy*/
int NumOfRecs(FILE *);/*goes throug a given file and counts num of records*/
int AllocateFile(FILE *,long );/*allocate each contents of file to struct*/
int main(void){
FILE *filename;/*for getfile return value*/
long total_records;/*for num of records return value*/
filename=GetFile();/*retuns the address of a file so can be worked on*/
total_records=NumOfRecs(filename);/*gives us how many lines in the file*/
AllocateFile(filename,total_records);/*put all text content to the struct*/
getchar();
return 0;
}
/*function definitions */
FILE *GetFile(void){
/*first thing to do is get the name of the file to parse and work on*/
char filename[MAX];
char *find;
FILE *fp;
/*this should be a loop until a proper file name is given*/
puts("What is the name of the file and its extension");
/*should also get additional info like how many fields are in the file*/
while( (fgets(filename,MAX,stdin)) !=NULL && filename[0]!='\n'){
if( (find=strchr(filename,'\n'))!=NULL)
*find='\0';
if ( (fp=fopen(filename,"r")) == NULL){
perror("error:");
puts("enter file name or newline to quit:");
}
else{
puts("file opened");
return fp;
}
}/*end of while, return null since unable to open file */
return NULL;
/*this crashes if a users presses a '\n' why? trace program state*/
}
int NumOfRecs(FILE *thefile){
/*the file is already opened and its addy is passed as a parameter to each funtion
so just work on it*/
long records=1;/*i think it starts counting at zero so add 1 for the first line*/
int character;/*what character looking at*/
while( (character = fgetc(thefile)) !=EOF){
if ( (character == '\n'))
records+=1;
}
fprintf(stdout,"num of records in file is %ld\n",records);
return records;
}
int AllocateFile(FILE *filename,long num_of_records){
/*iterates through file contents and adds each character to the appropiate field*/
int ndx=0;
int field=0;/*ndx for field ndx*/
size_t word_length=0;
int character;
int comma_count=0;
char buffer[BUFSIZ];/*really holding integers should put it to char*/
RECORD person[num_of_records];/*array of records named person to hold each line in file*/
/*go through file and puts contents in the array*/
while ( (character=fgetc(filename)) !=EOF){
buffer[ndx++]=character; /*test*/putchar(character);
word_length+=1;
if ( (buffer[ndx] ==',') ){
buffer[ndx]='\0';/*terminate and make buffer a string*/
comma_count+=1;/*update comma count so know where we are in file*/
switch(comma_count){
case 1:
strncpy(person[field].field1,buffer,word_length);
ndx=0 ;/*buffer ndx goes to zero*/
word_length=0;
break;
}
}
}
}