Opening files

Can anyone post the code or email me a code that will allow me to open a file through the command prompt. I'm using dev c++ can can not figure out how to write it. please help me out and if not to much trouble write what each line does "//"

I'm semi new to programming but when I get the code will it show me a list of files I can open or must I know the exact file name. If so can I get a code the lets me target files? I got a little something like this so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
#include <iostream>
#include <string>
using namespace std;

int main ()


{
    system ("TITLE Test File Open");
    system ("COLOR A");
    
    string filenm;
    char cDoagain;
    char YesNo;


  do
  {
          system ("TITLE K1 Conqueror");
          system ("CLS");
            
          cout << "Kruc1fix Conqueror hack\n\n";
          cout << "Name of file requesting access to: ";
          cin >> filenm;
          cout << filenm << " is this the correct file? (Y or N) ";
          cin >> YesNo;
      switch (YesNo)
      {
         case 'Y' :                           
         case 'y' :          
          cout << "Executing file hack\n\n";
                  FILE *newfile; 
                  newfile=fopen("c:\", "r");
                  system ("PAUSE");
                   break;
         case 'N' :
         case 'n' :
              cout << "Redo program? (Y or N) ";
              cin >> cDoagain;
      } 
      }        
      while  ((cDoagain != 'N')&&(cDoagain != 'n'));     
       
           system ("PAUSE");
           return 0;
} 
Last edited on
Compile the code, and run the exe file with a filename in command mode.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main(int argc, char *argv[])
{
	FILE * pFile;
	long lSize;
	char * buffer;
	size_t result;
	system("dir");

	printf("%d\n",argc);
//	argv[1] = (char*)&("test.txt");
	printf("%s\n",argv[1]);

	//strcpy(argv[1],"test.txt");
	if (argc<2)
	{
		puts("run program with a paramater\n");
	}
	else
	{
		pFile = fopen(argv[1],"r");

		if (pFile==NULL) {fputs ("File error",stderr); exit (1);}                              

		// obtain file size:
		fseek (pFile , 0 , SEEK_END);
		lSize = ftell (pFile);
		rewind (pFile);

		// allocate memory to contain the whole file:
		buffer = (char*) malloc (sizeof(char)*lSize);
		if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

		result = fread(buffer,(size_t)1,lSize,pFile);                                           

		puts(buffer);

		// terminate
		fclose (pFile);
		free (buffer);
	}
	getchar();
	return 0;
}
thanks! How do I switch to command mode? Also this only pops programs I've already written through dev c++. How do I back it up to my hard drive and then just select which file or folders I want access to.
Last edited on
please, make it clear, i didnt get what you want
What do you mean with a filename in command mode? Also, that code only shows the saved files I've written in dev cpp. How do I search for a file/name a file?
Topic archived. No new replies allowed.