Ok I'm working on a class project for C programming and I'm having problems opening 2 data files I need. When I execute the code it gives me the error opening files message.

To execute I've been getting into the proper directory and then typing

finalproject population path
finalproject is the name of the .exe file
population and path are 2 seperate text files I need to open and scan into the program

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define FILENAME "h:\\destruction.txt"

int main(int argc, char *argv[])
{
	int i,c,d;
	for(i=0;i<argc;i++)
		printf("arg %d: %s\n", i, argv[i]);
	FILE *population = fopen(argv[1],"r");
	if(population==NULL)
		printf("Error opening population matrix file, try again \n");
	else
	{
		while((c=getc(population))!=EOF)
		putchar(c);
	}
	FILE *path=fopen(argv[2],"r");
	if(path==NULL)
		printf("Error opening path matrix file, try again \n");
	else
	{
		while((d=getc(path))!=EOF)
		putchar(d);
	}

    int a,b;
    int destruction[5][5];
    FILE *des;
	printf("\nDestruction matrix is as follows\n");
    if((des=fopen(FILENAME,"r"))==NULL)
	    printf("The file was not opened, HAHA\n");
	for(a=0;a<5;a++)
        fscanf(des,"%d %d %d %d %d",&destruction[a][0],&destruction[a][1],&destruction[a][2],&destruction[a][3],&destruction[a][4]);
		/*print out the destruction matrix*/
        for(a=0;a<5;a++)
        {
            for(b=0;b<5;b++)
			            printf("%d ",destruction[a][b]);
			printf("\n");
        }
 }

Any help or suggestions would be greatly appreciated.


Thanks

Recommended Answers

All 5 Replies

It's [/code], not [\code]. :)

You're declaring variables in the middle of a block, which is C++/C99.

if((des=fopen(FILENAME,"r"))==NULL)
printf("The file was not opened, HAHA\n");

Might want to exit at this point.

When I execute the code it gives me the error opening files message.

Which message?

Perhaps the file doesn't exist. Make sure it does.

It's [/code], not [\code]. :)

You're declaring variables in the middle of a block, which is C++/C99.

if((des=fopen(FILENAME,"r"))==NULL)
printf("The file was not opened, HAHA\n");

Might want to exit at this point.


Which message?

Perhaps the file doesn't exist. Make sure it does.

The errors that its giving me is the "Error opening population matrix, try again" and the "Error opening path matrix, try again" errors, the "des" matrix opens fine. I've got 2 seperate .txt files in the debug folder of the project, one named population.txt, and the other path.txt, and I'm having issues getting those to load.

Does your filename or path contain a space? If so, make sure to pass it inside double quotes...

Does your filename or path contain a space? If so, make sure to pass it inside double quotes...

The path does contain a space, so then should it be like this

int i,c,d;
	for(i=0;i<argc;i++)
		printf("arg %d: %s\n", i, argv[i]);
	FILE *population = fopen("filepath"argv[1],"r");
	if(population==NULL)
		printf("Error opening population matrix file, try again \n");

NO - you missed my point - I meant when RUNNING the program, not the code for example. Instead of:

MyProg this is my path this is my file.txt

you should run:

MyProg "this is my path" "this is my file.txt"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.