PDA

View Full Version : First C programming project - can you check?


spadez
02-07-2009, 06:33 PM
Hi,

Im having some problems when I compile, can someone please check this code for me?


#include <stdio.h>
#include "stdafx.h"

int main(int argc, char ** argv){
int c;
FILE * fp;

if(argc < 2){
printf("Usage:\n\t%s filename\n",argv[0]);
return -1;
}

if((fp = fopen(argv[1],"rb")) == NULL){
printf("can't open %s\n",argv[1]);
return -2;
}

while((c = fgetc(fp)) != EOF){
#define MAX_LINE_LEN 1024*512 /* 1/2 mega byte, should be more than sufficient */
.....

char line[MAX_LINE_LEN];
int len=0;
int cnt_of_fields=0;
char *p;
while( (c=GET_A_CHAR_FROM_FILE(fp))!=EOF){
switch(c)
{
case '"':
case '\'':
/* parse a quoted string, ignore it for now */
break;
case ',':
++cnt_of_fields; /* a comma signal end of previous field and begining of next fields */
line[len++]='\0';
if(len==MAX_LINE_LEN){
fprintf(stderr, "Line too long\n");
exit(-1);
}
break;
case '\n':
++cnt_of_fields; /* a EOL is end of record, and at the same time end of field */
line[len++]='\0';

/* make a copy of the line in the heap, note strdup or strcpy won't work in our case */
p = (char *)malloc(len);
memcpy(p, line, len); /* now all the fields in the record are stored in p[ ] */
add_a_record( p );
break;
default:
line[len++] = c;
if(len==MAX_LINE_LEN){
fprintf(stderr, "Line too long\n");
exit(-1);
}
}
}
printf( "%s", line );
}

fclose(fp);
return 0;
}



Im trying to read a CSV file from the program and then call lines.

oracleguy
02-07-2009, 11:36 PM
What errors are you getting back from the compiler?

spadez
02-07-2009, 11:44 PM
These are the errors im left with.


Warning 1
Warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Quote:
Error 2
Error C3861: 'GET_A_CHAR_FROM_FILE': identifier not found

Quote:
Error 3
Fatal error C1903: unable to recover from previous error(s); stopping compilation

oracleguy
02-08-2009, 01:26 AM
The first one is a warning, your real problem is the error.

Like it says, GET_A_CHAR_FROM_FILE isn't defined. You are using it like a function, have you not defined that function?

spadez
02-08-2009, 01:44 AM
I assume not, how do I define the function?

ralph l mayo
02-08-2009, 04:15 AM
Just change it to fgetc(fp) as in the previous loop. It's not clear to me why you have two while loops with identical conditions, but that's another issue.