Can't Open the program.

This is a discussion on Can't Open the program. within the C Programming forums, part of the General Programming Boards category; I been working on a sample problem from our text book and I can't open the files I'm referencing can ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    69

    Can't Open the program.

    I been working on a sample problem from our text book and I can't open the files I'm referencing can some help me, and explain what it is I need to do to be able to run this program.

    Thank you in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_LINES_PER_PAGE  50
    #define BUFFER_SIZE        133
    #define FORM_FEED          '\f'
    
    //	Type Declarations
    typedef struct stuData
    	{
    	 char  name[26];
    	 char  id[5];
    	 int   exams[3];
    	 int   problems[8];
    	 char  grade;
    	} STU_DATA;
    
    //	Function Declarations
    STU_DATA getData      (FILE* binFile);
    void     writeReport  (STU_DATA aStudent,
                           FILE* prtFile);
    void     pageHeaders  (FILE* prtFile);
    
    int main (void)
    {
    //	Local Declarations
    	char      stuFileID[]  = "P13-stu.bin";
    	char      prtFileID[]  = "P13-stu.prt";
    	STU_DATA  aStudent;
    	FILE*     stuFile;
    	FILE*     prtFile;
    
    //	Statements
    	printf("\nBegin Student Report Creation\n ");
    
    	if(!(stuFile = fopen(stuFileID, "rb")))
    	   {
    	    printf("\nCannot open %s\n", stuFileID);
    		system("Pause");
    	    exit (100);
    	   } // if stuFile
    	if (!(prtFile = fopen(prtFileID, "w"))) 
    	   {
    	    printf("\nCannot open %s\n", prtFileID);
    		system("Pause");
    	    exit (200);
    	   } // if prtFile
    	
    	aStudent = getData (stuFile);
    	while (!feof(stuFile))
    	   {
    	    writeReport (aStudent, prtFile);
    	    aStudent = getData (stuFile);
    	   } // while
    
    	fprintf(prtFile, "\nEnd of Report\n"); 
    	fclose(stuFile);
    	fclose(prtFile);
    	printf("\n\nEnd Student Report Creation\n");
    	system("Pause");
    	return 0;
    }	// main
    
    /*	==================== getData ====================
    	This function reads the student binary file.
    	   Pre   stuFile is opened for reading 
    	   Post  one student record read and returned 
    */
    STU_DATA getData (FILE* stuFile) 
    {
    //	Local Declarations
    	int       ioResult;
    	STU_DATA  aStu;
    
    //	Statements
    	ioResult = fread(&aStu, 
    	                 sizeof(STU_DATA), 1, stuFile);
    	if (!ioResult)
    	   if (!feof(stuFile))
    	      {
    	       printf("\n\nError reading student file\n");
    	       exit (100);
    	      } // if !feof
    	return aStu;
    } // getData
    
    /*	=================== writeReport ====================
    	Write student report to a text file.
    	   Pre   prtFile is opened as a text output file
    	         aStudent is complete
    	   Post  Report line written 
    */
    void writeReport (STU_DATA  aStu, FILE* prtFile)
    {
    //	Local Declarations
    	static int lineCount = MAX_LINES_PER_PAGE + 1;
    	char   buffer[BUFFER_SIZE];
    
    //	Statements
    	if (++lineCount > MAX_LINES_PER_PAGE)
    	   {
    	    pageHeaders  (prtFile);
    	    lineCount = 1;
    	   } // if
    
    	sprintf (buffer, 
    	  "%-25s %4s %4d%4d%4d%4d%4d%4d%4d%4d%4d%4d%4d %c\n", 
    	  aStu.name, aStu.id, 
    	  aStu.exams[0],    aStu.exams[1],    aStu.exams[2],
    	  aStu.problems[0], aStu.problems[1], aStu.problems[2], 
    	  aStu.problems[3], aStu.problems[4], aStu.problems[5], 
    	  aStu.problems[6], aStu.problems[7], 
    	  aStu.grade);
    	fputs (buffer, prtFile);
    	return;
    } // writeReport
    
    /*	================== pageHeaders ==================
    	Writes the page headers for the student report.
    	   Pre   prtFile is opened as a text output file
    	   Post  Report headers and captions written
    */
    void pageHeaders (FILE* prtFile) 
    {
    //	Local Declarations
    	static int pageNo = 0;
    
    //	Statements
    	pageNo++;
    	fprintf(prtFile, "%c", FORM_FEED);
    	fprintf(prtFile, "%-66s  Page %4d\n",
    	        "Student Report ", pageNo);
    	fprintf(prtFile, "%-25s %-6s %-10s %-27s Grade\n\n",
    	        "Student Name", "ID", "Exams", "Problems");
    	return;
    }	// pageHeaders
    //	================= End of Program =================
    Here is the data that is use for student files. One is labeled .bin &.prt.

    P13-stu.prt contains:
    Student Report Page 1
    Student Name ID Exams Problems Grade

    First 1111 11 12 13 111 112 113 114 115 116 117 118 A
    Second 2222 21 22 23 221 222 223 224 225 226 227 228 B
    Third 3333 31 32 33 331 332 333 334 335 336 337 338 C
    Fourth 4444 41 42 43 441 442 443 444 445 446 447 448 D
    Last 5555 51 52 53 551 552 553 554 555 556 557 558 F

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that the files are in the current working directory when you run your code [note that for example visual studio will run the application in the "debug" or "release" directory of the project].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to open IE
    By x2012 in forum C# Programming
    Replies: 2
    Last Post: 11-03-2006, 09:07 PM
  2. File Types Automatically Open With My Program
    By ElWhapo in forum Windows Programming
    Replies: 3
    Last Post: 12-29-2004, 05:39 PM
  3. how to make certain file types open IN your program
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-06-2003, 11:37 PM
  4. drawing over the open program.
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-23-2002, 03:37 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21