Thread: Trouble with opening files

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    Trouble with opening files

    I'm currently taking a C programming class over the summer right now. I'm using Visual Studio 2010. I really enjoy the class and am doing really well in it but I have a problem opening files in visual studio. I tried writing a program and I keep getting problems creating, opening files. I checked, double-checked, umpteenth-checked my code and it seems okay.

    Then I tried even the most basic file opening programs and it still won't open the dang file.

    This is merely a practice program. I took it directly from the book. The code compiles successfully but when it runs. Without fail it goes to the Error opening student file

    Code:
    #include <stdio.h>
    
    
    int getstu(FILE* spstu, int* stuid, int* exam1, int* exam2, int* final);
    int writestu(FILE* spgrades, int stuid, int average, char grade);
    void calcgrade(int exam1, int exam2, int final, int* average, char* grade);
    
    
    int main(void)
    {
    	FILE* spstu;
    	FILE* spgrades;
    
    
    	int stuid;
    	int exam1;
    	int exam2;
    	int final;
    	int average;
    
    
    	char grade;
    
    
    	printf("Begin student grading\n");
    	if (!(spstu = fopen ("P07-06.DAT", "r")))
    	{
    		printf("\aError opening student file\n");
    		return 100;
    	}
    
    
    	if (!(spgrades = fopen ("P07-06Gr.DAT", "w")))
    	{
    		printf("\aError opening grades file\n");
    		return 102;
    	}
    
    
    	while (getstu(spstu, &stuid, &exam1, &exam2, &final))
    	{
    		calcgrade(exam1, exam2, final, &average, &grade);
    		writestu(spgrades, stuid, average, grade);
    	}
    
    
    	fclose(spstu);
    	fclose(spgrades);
    
    
    	printf("End student grading\n");
    	return 0;
    }
    
    
    int getstu(FILE* spstu, int* stuid, int* exam1, int* exam2, int* final)
    {
    	int ioresult;
    	ioresult = fscanf(spstu, "%d%d%d%d", stuid, exam1, exam2, final);
    	if(ioresult == EOF)
    		return 0;
    	else if(ioresult != 4)
    	{
    		printf("\aError reading data\n");
    		return 0;
    	}
    	else
    		return 1;
    }
    
    
    void calcgrade(int exam1, int exam2, int final, int *average, char* grade)
    {
    	*average = (exam1 + exam2 + final) / 3;
    	if (*average >= 90)
    		*grade = 'A';
    	else if(*average >= 80)
    		*grade = 'B';
    	else if(*average >= 70)
    		*grade = 'C';
    	else if(*average >= 60)
    		* grade = 'D';
    	else
    		*grade = 'F';
    	return;
    }
    
    
    int writestu(FILE* spgrades, int stuid, int average, char grade)
    {
    	fprintf(spgrades, "%04d %d %c\n", stuid, average, grade);
    	return 0;
    }
    Maybe I need a better understanding of files but my book isn't very helpful. I think it (maybe?) might be a setting in Visual Studio I have to change????

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    I should get rid of those "\a"s those guys are annoying me...

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes.The problem is that VS can not find the file you ask for.The fopen function returns NULL(as it ought to do),because the file requested was not found.I think you should put a suitable path on your project in order to find the file.Search the google Or wait until a VS user sees that post

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Ahhhh... I see. So I must actually write a text file in the directory named P07-06.DAT?

  5. #5
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by pakjunho View Post
    Ahhhh... I see. So I must actually write a text file in the directory named P07-06.DAT?
    Is P07-06.DAT a file or a directory? If it is a directory, then you need a file in the directory, and include the full path of the directory and file as an argument in fopen().To keep it simple(for a beginner), just store the file that you want to open in the same location as your C program file. Then you don't need to include the path to open the file, just the file name.So if you have file.txt, to open you would write:
    Code:
    if(!(spstu = fopen("file.txt", "r")))
    And if file.txt was in the P07-06.DAT directory, it would look something like:
    Code:
    if(!(spstu = fopen("c://P07-06.DAT//file.txt", "r")))
    assuming that c:/P07-06.DAT really exists. You would need to use \\ if you were using Windows.What std10093 means is that there is a setting in Visual Studio(VS) that allows you to set the paths for your projects. So that your code only needs:
    Code:
    if(!(spstu = fopen("file.txt", "r")))
    in order to open the file. VS knows the location of the files, so you don't need to include the full path(c://P07-06.DAT//) of the file in your code.
    Last edited by cfanatic; 08-04-2012 at 12:43 AM.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    cfanatic: the paths in VS are for the build steps. They tell the compiler and linker where to find source files, header files, object files, library files, resource files, etc. The program itself doesn't automatically learn about these paths from Visual Studio.

    Also you mention using \\ in string literals for path separators for Windows, which is correct, but your examples use //, which is not correct. Slash and backslash are two completely different symbols in C. Of course, everytime I've had the "pleasure" (more like pain) of coding for Windows I alway use a single slash as a path separator, and it works fine. I think all the C file functions in Windows accept either backslashes or slashes. I also prefer slashes because they are The One True Path Separator . After all, the Web uses slash, Unix uses slash, MacOS X uses slash (well, that's Unix too), etc.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by christop View Post
    cfanatic: the paths in VS are for the build steps. They tell the compiler and linker where to find source files, header files, object files, library files, resource files, etc. The program itself doesn't automatically learn about these paths from Visual Studio.
    Actually there is a setting in the VS IDE called something like working directory. When the program is run from the IDE then the current working directory is set to that path before execution. This way you can use relative path in your programs. Beware that this path can be set to different locations for debug and release builds.

    Kurt

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by ZuK View Post
    Actually there is a setting in the VS IDE called something like working directory. When the program is run from the IDE then the current working directory is set to that path before execution. This way you can use relative path in your programs. Beware that this path can be set to different locations for debug and release builds.

    Kurt
    Well, I stand corrected. I guess it does make sense to have a setting for the working directory while running the program too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening .c files
    By ss1david1ss in forum C Programming
    Replies: 4
    Last Post: 01-23-2007, 11:13 PM
  2. Having Trouble Opening My Dialog Box
    By ElWhapo in forum Windows Programming
    Replies: 8
    Last Post: 12-28-2004, 12:13 PM
  3. Opening files...
    By prestomrmime in forum C Programming
    Replies: 4
    Last Post: 07-08-2003, 08:14 PM
  4. trouble opening a text file
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2002, 09:11 PM
  5. saving files/opening files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2001, 10:16 PM

Tags for this Thread