Thread: C program for reading text files and calculating and store in another file

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    C program for reading text files and calculating and store in another file

    how to program this one
    Usage of Arrays, Functions and Files
    Students are expected to write, compile, run and test a complete C program to evaluate multiple-choice examination results of the students. For that purpose, three text files are provided involving student’s information, answers, and the solution key. Solution key ‘booklet.txt’ contains three rows containing correct answers for three different booklet types, namely, A, B, and C. Student’s answers are given in ‘answers.txt’. Student’s departmental information is given in ‘students.txt’. The program shall read all three files and produce a new file called ‘results.txt’ containing the results in the given format.
    ‘booklet.txt’ content looks like below (each booklet type has 50 correct answers)
    ACBAA*DBC*DDAACDBACCABDCABCCBDDABC*CABABABCBD*AABD



    ‘answers.txt’ content looks like below (* means unanswered)
    117429 A DBA*DCACBACDBABDCABBCADACCBBDDAABC*CABABCDBDC*AABD
    124259 B D*DBCBAAC*CBACBBDA*BDA*CCABAACDBABBCAADD*CBD*AABCA

    ‘students.txt’ content looks like below (ID, name-surname, and department)
    117429 MEHMET AKYOL SENG
    124259 EMRE YASAR EEE

    Write a C program that makes use the following functions to be defined:
    a) read keys for three booklet types into corresponding array
    b) read one student information into corresponding arrays
    c) read one student answers into corresponding array
    d) evaluation of one student answers
    e) statistics on student’s answers
    ‘results.txt’ file content may look like as below (4 wrong answer cancels a correct answer!)
    PART-A (Results)
    Std. No. Name & Surname Department Correct Wrong Score
    117429 MEHMET AKYOL SENG 28 20 46.0

    PART-B (Statistics)
    Booklet-A
    Question Correct choice A B C D Blank Success
    1 D 3 2 4 5 1 %33.3
    2 B 5 9 1 7 2 %37.5

    Booklet-B

    Booklet-C

    how to do this one
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    We expect you to make an effort to solve the problem. We can help, but we're not here to hand over complete answers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    2
    i know about this one wanted to be sure
    Code:
    #include<stdio.h>
    void readbooklet(char[], char[], char[]);
    void readdata(char[],char[],char[]);
    int main()
    {
    	int empty=0,wrong=0,correct=0;
    	float score=0;
    	int i=0,j=0;
    	char book_a[50],book_b[50],book_c[50];//answer booklet a ,b, c;
    	char an_no[20][10],an_type[20],an_answers[20][50]; //std answers
    	char no[20][10], name[20][30],depart[20][10];
    	
    	readbooklet(book_a,book_b,book_c);
    	readdata(no,name,depart);
    	
    	
    /*	int A=0; int B=0;
    	for(i=0;i<50;i++)
    	{
    		if(book_a[i]=='A')
    			A=A+1;
    		if(book_a[i]=='B')
    			B=B+1;	
    	}
    	printf("a=%d b=%d",A,B);
    */
    	FILE *answers;
    	FILE *info;
    	FILE *write;
    	write=fopen("D:\\try1.txt","w");
    	fprintf(write,"std. No.\tName & Surname\t\t\tDepartment\tcorrect\twrong score\n");//spaces for std no 15 space name dep,10 space dep
    	
    	answers=fopen("D:\\answers.txt","r");
    	if(answers!=NULL)
    	{
    		while(feof(answers))
    		{
    			fscanf(answers,"%s %s %s",&an_no[i],&an_type[i],&an_answers[i]);
    			i++;
    		}
    	}
    	
    }
    void readbooklet(char a[],char b[], char c[])
    {
    	FILE *booklet; 
    	booklet=fopen("D:\\booklet.txt","r");
    	int i=0;
    	{
    		if(booklet != NULL)
    		{
    			while(!feof(booklet))//file end of file
    			{
    				if(i==0)
    				{
    					fscanf(booklet,"%s", a);
    					i++;
    	
    				}
    				else if(i==1)
    				{
    				fscanf(booklet,"%s",b);
    				i++;
    				}
    				else
    				{
    					fscanf(booklet,"%s",c);
    				}
    			}
    		}
    		else
    			printf("we cant find this file!");
    	}
    }
    			
    		
    
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Where is readdata?

    Does it compile? I think not, there seems to be a space brace as line 79.

    > while(feof(answers))
    This is wrong.
    feof() is never true immediattely after opening a file, so your loop does nothing.

    Better would be (assuming a well-formed file)
    Code:
    while ( fscanf(answers,"%s %s %s",&an_no[i],&an_type[i],&an_answers[i]) == 3 ) {
        i++;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to store contents from a text file into arrays?
    By JohnRaphael in forum C Programming
    Replies: 3
    Last Post: 03-19-2016, 03:12 AM
  2. Reading encrypted text file in program!
    By xakzyy in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2013, 12:11 PM
  3. Read text file and store it in an array
    By look2hook in forum C Programming
    Replies: 2
    Last Post: 12-03-2010, 11:47 PM
  4. reading text from a file into your program
    By deltabird in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2003, 10:34 AM

Tags for this Thread