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...

Parse errors can be confusing, but if you posted your code exactly as it is, the declaration of open_file() in your header is missing a semicolon at the end. Since headers are textually inserted into the translation unit, this

#include "functions.h"
 
int open_file(int argc, char argv[], int count)
{
    ...

becomes this

int open_file(int argc, char argv[], int count)
 
int open_file(int argc, char argv[], int count)
{
    ...

And the error is easily spotted. As such, my advice is to start with a single source file, then break it up into headers. That way you can be sure that the code compiles and works together, so any errors after breaking it up are easier to figure out.

>>string_size = sizeof(argv[count]);

Line 20 of main(). sizeof returns the size of the argument, not the length of the string. Since argv[count] is just a pointer, sizeof(argv[count]) is the same as sizeof(char*) which is normally 4 on most 32-bit compilers today.

What you probably want is string_size = strlen(argv[count]);

This article has been dead for over six months. Start a new discussion instead.