I want to read he source file of the .exe I am writing.
For example if I am running test.exe I want to read test.c Also if the name of the file changes it still had to open it.
what i have tried is:

FILE *fp=fopen(*.c,"r")

Can someone help me?
thanks
LEeba

Try investigating argv[0],,,example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_EXT ".c"

int main(int argc, char**argv)
{
	char filename[256];
	filename[0] = '\0';

	fprintf(stdout, "argv[0]->%s\n", argv[0]);

	/*strcat(filename, &argv[0][2]);*//*for linux dot slash ./filename*/
	strcat(filename, argv[0]);
	strcat(filename, FILE_EXT);

	fprintf(stdout, "filename->%s\n", filename);
	exit(EXIT_SUCCESS);
}

Thank you
WHen i write te command line to start the program what to i write for the variables that it will give me the program name.
Thanks I have always had problems with that.

Try investigating argv[0],,,example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_EXT ".c"

int main(int argc, char**argv)
{
	char filename[256];
	filename[0] = '\0';

	fprintf(stdout, "argv[0]->%s\n", argv[0]);

	/*strcat(filename, &argv[0][2]);*//*for linux dot slash ./filename*/
	strcat(filename, argv[0]);
	strcat(filename, FILE_EXT);

	fprintf(stdout, "filename->%s\n", filename);
	exit(EXIT_SUCCESS);
}

The operating system populates argv[0] -- you don't have to do anything. When you type an argument on the command line it becomes argv[1], not argv[0].

OK I am leaving the house now but am going to try on the way to work on it.
Thanks all.

The operating system populates argv[0] -- you don't have to do anything. When you type an argument on the command line it becomes argv[1], not argv[0].

I know I only asked about opening unknown file btu what i really need is then to print the contents to the console. I thought I would manage but I am having problems. I keep getting that original = NULL.
Here si my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_EXT ".c"

int main(int argc, char **argv)
{
	char fileName[256];
	
	FILE *original;
	char fileContent;
	fileName[0]='\0';
	
	strcat(fileName, argv[0]);
	strcat(fileName, FILE_EXT);
	
	
	original = fopen(fileName,"r");
	
	if(original==NULL){
		printf("Can't open file for reading");
		return 1;
	}
	fileContent=fgetc(original);
	while (fileContent!=EOF)
	{
		putchar(fileContent);
		fileContent=fgetc(original);
	}
	fclose(original);
	return 0;
	
}

I just realized the problem. I used the debugging and realized that it is using source.exe.c . I will try seperating just the source and then adding .c.

I got now only source but it comes up in the debug folder. HOw do I get otu of the debug folder if I don't know the exact place the file is located? Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILE_EXT ".c"

int main(int argc, char **argv)
{
	char fileName[256];
	char *tok;
	FILE *original;
	char fileContent;
	fileName[0]='\0';
	
    tok = strtok(argv[0], ".");
    tok = strtok(NULL, ".");
	
	strcat(fileName, "/");
	strcat(fileName,argv[0]);
	strcat(fileName, FILE_EXT);
	
	
	original = fopen(fileName,"r");
	
	if(original==NULL){
		printf("Can't open file for reading");
		return 1;
	}
	fileContent=fgetc(original);
	while (fileContent!=EOF)
	{
		putchar(fileContent);
		fileContent=fgetc(original);
	}
	fclose(original);
	return 0;
	
}

Depending on the operating system, argv[0] may contain the full path to the program. It does that when run under MS-Windows, but may not when run under other operating systems.

I finally found the problem. I got it fixed in linux.
Thanks everyone.

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.