I have to open and read a file whos file name is provided through a command line argument. Right now, I am using the following method.
My main:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "functions.h"
int main(int argc, char *argv[])
{
int count, string_size;
int other = argc;
//SHOW my_show_array;
char *temp_char, *file_name;
if (argc > 1)
{
printf("File names have been provided and will be loaded in the given order:\n\n");
for(count = 1; count < argc; count++)
{
string_size = sizeof(argv[count]);
//file_name[string_size]=*argv[count];
printf("%s\n", argv[count]);
}
for(count = 1; count < argc; count++)
{
open_file(argc, *argv[count], count);
}
}
return 0;
}
Followed by a header file b/c I have to split up the files(per professor's requirement)
int open_file(int argc, char argv[], int count)
And finally the file that contains my function:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "functions.h"
int open_file(int argc, char argv[], int count)
{
int count=1, nbytes = 300;
FILE *file;
char delims[] = " ";
char *line_buffer, *p;
if (argc>1)
{
file=fopen(argv,"r");
if(file!=NULL)
{
line_buffer = (char *) malloc(nbytes + 1);
getline(&line_buffer, &nbytes, file);
/*
p = strtok(line_buffer, delims);
while(p!=NULL)
{
printf("%s\n", p);
p = strtok(NULL, " ");
}
*/
}
}
}
I'm having completely different problems with parsing the line, but my main focus here is when i try and compile these three files together, I am receiving these errors,
Lab4.c: In function âopen_fileâ:
Lab4.c:9: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before â{â token
Lab4.c:32: error: expected â{â at end of input
functions.h: In function âopen_fileâ:
functions.h:1: error: expected â{â at end of input
functions.c: In function âopen_fileâ:
functions.c:9: error: expected declaration specifiers before âopen_fileâ
functions.c:34: error: expected â{â at end of input
These seem to be kind of vague errors, referring to the beginning and ending of each function...I'm sure its something simple but I just can't see it for the life of me...