Urgent! Pls help me for my assignment!!

This is a discussion on Urgent! Pls help me for my assignment!! within the C Programming forums, part of the General Programming Boards category; 1)Is it possible to write a programe in c, which the user can create and name text file themselve? 2)How ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Exclamation Urgent! Pls help me for my assignment!!

    1)Is it possible to write a programe in c, which the user can create and name text file themselve?

    2)How to make a header and footer for our programme in c, which is sth as below?
    The cursor is between the header and footer.

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    Please enter your matrix number: _









    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    1)Is it possible to write a programe in c, which the user can create and name text file themselve?
    Yes. You just get a string from the user and then open a file with that string in mode "w" .

    2)How to make a header and footer for our programme in c, which is sth as below?
    The cursor is between the header and footer.
    That isn't covered by standard C. You would need to use ncurses or something similar. What compiler / system are you using?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Exclamation Thanks for reply^^

    Thanks for reply^^
    Yup. yup....it works^^
    I'm able to create and name the text file ady....
    But the data that i get from user cannot fprintf into text file.....somemore when read the data, it won't stop......below is my code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void)
    {
    	int option;
    	char textname [50];
    	char outputname [50];
    	char userinput [100];
    	char textfile [100];
    	FILE* spout;
    	FILE* sptext;
    
    	printf("(1)	Create File\n");
    	printf("(2)	Write to File\n");
    	printf("(3)	Read from File\n");
    	printf("(4)	Quit\n");
    
    	printf("\nPlease select your option <1-4>: ");
    	scanf("%d", &option);
    
    	if (option == 1 || option == 2)
    	{
    		printf("Please name your file: ");
    		scanf("%s", outputname);
    
    		if (!(spout=fopen(outputname, "w")))
    		{
    			printf("\aError creating file!\n");
    			return 0;
    		}
    
    		spout = fopen(outputname, "r");
    
    		printf("File was created successfully!\nTo stop, please key [END].\nPlease key in your data:\n");
    
    		while (strcmp(userinput,"END")!=0)
    		{
    			scanf ("%s", userinput);
    			fprintf (spout,"%s",userinput);
    		}
    
    		if (fclose(spout)==EOF)
    		{
    			printf("\aError closing file!\n");
    			return 0;
    		}
    		fclose(spout);
    		printf("File was closed successfully!\n");
    	}
    
    
    	else if (option == 3)
    	{
    		printf("Please enter name of file: ");
    		scanf("%s", textname);
    
    		if (!(sptext=fopen(textname, "r")))
    		{
    			printf("\aError opening file!\n");
    			return 0;
    		}
    
    		sptext = fopen(textname, "r");
    
    		printf("File was opened successfully!\n");
    
    		while (userinput!=NULL)
    		{
    			fscanf (sptext, "%s", textfile);
    			printf ("%s\n", textfile);
    		}
    		fclose(sptext);
    	}
    
    	else if (option == 4)
    	{
    		printf("Programme end.\n");
    		return 0;
    	}
    
    	else
    	{
    		printf("\aWrong input!\nProgramme was terminated!");
    		return 0;
    	}
    
    	printf("Thanks for using this programme!\nHave a nice day.\nProgramme end.\n");
    	return 0;
    }
    I hope....
    1)I can write this programme by only using fprintf and fscanf(no fget and fput). 2)Using string to write the data into text file.
    Anyone pls help me....^^
    Thank you very much.....

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're opening the file for writing, and then you're also immediately opening it for reading. That could surely be a problem.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is an example of setting the position of the console cursor. It allows you to print anywhere on the console window:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <wincon.h>
    
    
    void Gotoxy(int, int);
    
    int main(void)  {
       int x, y;
       
       char *starlight = "Oh Be a Fine Girl Kiss Me";
       char *ilike = "I like blue stars";
       for(x = 0, y = 1; x < 41;)  {
          Gotoxy(x,y);
    	  printf("%s", ilike);  
    	  x += strlen(ilike)+1;
    	  
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_BLUE|BACKGROUND_RED|BACKGROUND_BLUE|BACKGROUND_GREEN);	
       printf("\n%s\n ", ilike); 
       for(y = 4, x = 10; y < 18; y+=5)  {
          Gotoxy(x, y);
          printf("%s", starlight); 
          SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_RED|BACKGROUND_RED);
       }
       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_RED|BACKGROUND_BLUE);	
       printf("\n\n\n%s", starlight);
       printf("\n\n");   
       return 0;
    }
    
    
    void Gotoxy(int x, int y) {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    
    //BLUE |GREEN|RED = White or Grey, depending on Intensity
    //All combo's are the same as mixing primary colors of light (not paint).
    "Oh be a fine girl, kiss me" is a mnemonic to help remember the basic classification of light from a star.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Exclamation Hihi.....Please help me to check...

    I have done my assignment....
    Below is my code.....
    Anyone please help to check and modify in C lang, if it got any bad point or problems...
    Thanks^^
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void menu2 (void);
    void sorting (int option);
    
    int main (void)
    {
    	int option, num, midterm, final, i;
    	char ans;
    	
    	char filename [50];
    	char matrix [20];
    
    	FILE* spout;
    	do
    	{
    	system("cls");
    	printf("\t\t ***  Students' Marks Grading Program  ***\n\n\n\n\n");
    	printf("Do you have an input text file for students' marks?\n\n");
    	printf("1)No. I want to create an input text file.\n");
    	printf("2)Yes. I have.\n");
    	printf("3)Quit.");
    	printf("\n\n\nPlease select your option, eg. [1]: ");
    	scanf("%d", &option);
    	system("cls");
    
    	if (option == 1)
    	{
    		printf("Please name your input file, eg. [studentsmark.txt]: ");
    		scanf("%s", filename);
    
    		if (!(spout=fopen(filename, "w")))
    		{
    			printf("\aError creating file!\n");
    			return 0;
    		}
    
    		spout = fopen(filename, "w");
    
    		printf("File was created successfully!\n");
    		printf("Please enter the number of student: ");
    		scanf("%d", &num);
    		fprintf (spout,"01234012345678901234567890123456789\n");
    		fprintf (spout,"No\tMatrix\t\t\tMid term\tFinal\n");
    		for (i=0; i<num; i++)
    		{
    			printf("Student %d\n", i+1);
    			printf("Matrix no: ");
    			scanf ("%s", matrix);
    			printf("Mark of mid term: ");
    			scanf ("%d", &midterm);
    			printf("Mark of final: ");
    			scanf ("%d", &final);
    			
    			fprintf (spout,"%d\t%s\t\t%d\t\t%d\n",i+1,matrix,midterm,final);
    		}
    
    		if (fclose(spout)==EOF)
    		{
    			printf("\aError closing file!\n");
    			return 0;
    		}
    		fclose(spout);
    		printf("File was closed successfully!\n");
    	}
    
    
    	else if (option == 2)
    	{
    		menu2();
    		return 0;
    	}
    
    	else if (option == 3)
    	{
    		printf("Thanks for using this programme!\nHave a nice day.\n\n\nProgramme end.\n");
    		return 0;
    	}
    
    	else
    	{
    		printf("\aWrong input!\nProgramme was terminated!\n");
    		return 0;
    	}
    	printf("Do you want continue using this programme?\n");
    	printf("Press [Y] for yes, else press any key to abort\nYour choice: ");
    	fflush(stdin);
    	scanf("%c", &ans);
    	}while (ans == 'Y'|| ans =='y');
    
    	printf("Thanks for using this programme!\nHave a nice day.\n");
    	return 0;
    }
    
    void menu2 (void)
    {
    	FILE* spRead;
    	FILE* spout;
    	char ans;
    	char matrix [100][20];
    	char filename[50];
    	char header [100];
    	char grade [100][5];
    	int i, j, tempn;
    	int count=0;
    	int stuNo [100], midterm[100], final[100], total[100];
    	int option1, option2;
    	char tempc [20];
    	char tempg [5];
    
    	printf("\t\t ***  Students' Marks Grading Program  ***\n\n\n\n\n");
    	printf("1)Read your input text file.\n");
    	printf("2)Back to pervious menu.\n");
    	printf("3)Quit.");
    	printf("\n\n\nPlease select your option, eg. [1]: ");
    	scanf("%d", &option1);
    	system("cls");
    
    	if (option1 == 1)
    	{
    
    		printf("Please enter your input file name, eg. [studentsmark.txt]: ");
    		scanf("%s", filename);
    		if (!(spRead = fopen(filename,"r")))
    		{
    			printf("\aError opening %s\n", filename);
    			return;
    		}
    		
    		fgets(header, sizeof(header),spRead);
    		fgets(header, sizeof(header),spRead);
    	
    
    		for (j=0; j<100; j++)
    		{
    			fscanf(spRead, "%d%s%d%d", &stuNo[j], matrix[j],&midterm[j],&final[j]);
    			if(stuNo[j]==j+1)
    			{
    				count++;
    			}
    		}
    		for(i=0; i<count; i++)
    		{
    			if(midterm[i]>100||midterm[i]<0)
    			{
    				printf("\n\aYou Key In Wrong Midterm Input For Student No.%d.\n",i+1);
    				printf("Please Check Again Your Input Value.Thank You!!\n");	
    			}
    			if(final[i]>100||final[i]<0)
    			{
    				printf("You Key In Wrong Final Input For Student No.%d.\n",i+1);
    				printf("Please Check Again Your Input Value.Thank You!!\n");
    			}
    		}	
    			
    		for(i=0;i<count;i++)
    		{
    			total[i] = midterm[i] + final[i];
    			if(total[i]>100)
    			{
    				printf("\aError in Marks For Student No.%d. (Total is More Than 100)\n",i+1);
    			}
    		}
    
    		for (i=0; i<count; i++)
    		{
    			if (total[i]>=80)
    			{
    				strcpy(grade[i],"A");
    			}
    
    			else if (total[i]>=70 && total[i]<80)
    			{
    				strcpy(grade[i],"A-");
    			}
    
    			else if (total[i]>=65 && total[i]<70)
    			{
    				strcpy(grade[i],"B+");
    			}
    
    			else if (total[i]>=60 && total[i]<65)
    			{
    				strcpy(grade[i],"B");
    			}
    
    			else if (total[i]>=55 && total[i]<60)
    			{
    				strcpy(grade[i],"B-");
    			}
    
    			else if (total[i]>=50 && total[i]<55)
    			{
    				strcpy(grade[i],"C+");
    			}
    
    			else if (total[i]>=45 && total[i]<50)
    			{
    				strcpy(grade[i],"C");
    			}
    
    			else if (total[i]>=40 && total[i]<45)
    			{
    				strcpy(grade[i],"C-");
    			}
    
    			else if (total[i]>=35 && total[i]<40)
    			{
    				strcpy(grade[i],"D+");
    			}
    
    			else if (total[i]>=0 && total[i]<35)
    			{
    				strcpy(grade[i],"D");
    			}
    		}
    		
    		printf("No\tMatrix\t\t\tMid term\tFinal\tTotal\tGrade\n");
    		for(i=0; i<count; i++)
    		{
    			printf("%d\t%-10s\t\t%d\t\t%d\t%d\t%s\n", stuNo[i], matrix[i], midterm[i], final[i], total[i], grade[i]);
    		}
    		
    
    			if (fclose(spRead)==EOF)
    		{
    			printf("\aError closing %s\n", filename);
    			printf("\aFile close with error\n");
    			return;
    		}
    		
    
    	printf("\n\n\n");
    	printf("Do you wish to sort your data and output to text file?\n\n");
    	printf("1)Yes. Sort by ascending matrix no.\n");
    	printf("2)Yes. Sort by descending matrix no.\n");
    	printf("3)Yes. Sort by ascending total mark.\n");
    	printf("4)Yes. Sort by descending total mark.\n");
    	printf("5)Quit.\n");
    	printf("\n\n\nPlease select your option, eg. [1]: ");
    	scanf("%d", &option2);
    	if (option2 == 5)
    	{
    		system ("cls");
    		printf("Thanks for using this programme!\nHave a nice day.\n\n");
    		return;
    	}
    	printf("Please name your output file, eg. [studentsmark.txt]: ");
    	scanf("%s", filename);
    
    	if (!(spout=fopen(filename, "w")))
    	{
    		printf("\aError creating file!\n");
    		return;
    	}
    
    	spout = fopen(filename, "w");
    	if (option2 == 1)
    	{
    		for (i=0; i<count-1; i++)
    		{
    			for (j=(i+1); j<count;j++)
    			{
    				if ((strcmp(matrix[i],matrix[j])>0))
    				{
    					strcpy(tempc, matrix[i]);
    					strcpy(matrix[i] , matrix[j]);
    					strcpy(matrix[j] , tempc);
    					tempn = total[i];
    					total[i] = total[j];
    					total[j] = tempn;
    					strcpy(tempg, grade[i]);
    					strcpy(grade[i] , grade[j]);
    					strcpy(grade[j] , tempg);
    				}
    			}
    		}
    	}
    	if (option2 == 2)
    	{
    		for (i=0; i<count-1; i++)
    		{
    			for (j=(i+1); j<count;j++)
    			{
    				if ((strcmp(matrix[i],matrix[j])<0))
    				{
    					strcpy(tempc, matrix[i]);
    					strcpy(matrix[i] , matrix[j]);
    					strcpy(matrix[j] , tempc);
    					tempn = total[i];
    					total[i] = total[j];
    					total[j] = tempn;
    					strcpy(tempg, grade[i]);
    					strcpy(grade[i] , grade[j]);
    					strcpy(grade[j] , tempg);
    				}
    			}
    		}
    	}
    	if (option2 == 3)
    	{
    		for (i=0; i<count-1; i++)
    		{
    			for (j=(i+1); j<count;j++)
    			{
    				if (total[i]>total[j])
    				{
    					strcpy(tempc, matrix[i]);
    					strcpy(matrix[i] , matrix[j]);
    					strcpy(matrix[j] , tempc);
    					tempn = total[i];
    					total[i] = total[j];
    					total[j] = tempn;
    					strcpy(tempg, grade[i]);
    					strcpy(grade[i] , grade[j]);
    					strcpy(grade[j] , tempg);
    				}
    			}
    		}
    	}
    	if (option2 == 4)
    	{
    		for (i=0; i<count-1; i++)
    		{
    			for (j=(i+1); j<count;j++)
    			{
    				if (total[i]<total[j])
    				{
    					strcpy(tempc, matrix[i]);
    					strcpy(matrix[i] , matrix[j]);
    					strcpy(matrix[j] , tempc);
    					tempn = total[i];
    					total[i] = total[j];
    					total[j] = tempn;
    					strcpy(tempg, grade[i]);
    					strcpy(grade[i] , grade[j]);
    					strcpy(grade[j] , tempg);
    				}
    			}
    		}
    	}
    	
    	system ("cls");
    	fprintf (spout,"01234012345678901234567890123456789\n");
    	fprintf(spout, "No\tMatrix\t\t\tTotal\tGrade\n");
    	printf("No\tMatrix\t\t\tTotal\tGrade\n");
    	for(i=0; i<count; i++)
    	{
    		fprintf(spout,"%d\t%-10s\t\t%d\t%s\n", stuNo[i], matrix[i], total[i], grade[i]);
    		printf("%d\t%-10s\t\t%d\t%s\n", stuNo[i], matrix[i], total[i], grade[i]);
    	}
    	if (fclose(spout)==EOF)
    		{
    			printf("\aError closing file!\n");
    			return;
    		}
    		fclose(spout);
    		printf("File was closed successfully!\n");
    
    	printf("Do you want continue using this programme?\n");
    	printf("Press [Y] for yes, else press any key to abort\nYour choice: ");
    	fflush(stdin);
    	scanf("%c", &ans);
    	if (ans=='y'||ans=='Y')
    	{
    		main();
    	}
    	
    }
    
    	else if (option1 == 2)
    	{
    		main ();
    	}
    
    	else if (option1 == 3)
    	{
    		printf("Thanks for using this programme!\nHave a nice day.\n\n\nProgramme end.\n");
    		return;
    	}
    
    	else
    	{
    		printf("\aWrong input!\nProgramme was terminated!\n");
    		return;
    	}
    
    	return;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. pls help me urgent..
    By intruder in forum C Programming
    Replies: 4
    Last Post: 01-13-2003, 04:41 AM
  4. Urgent Help required.. pls help ??
    By intruder in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2003, 01:05 PM
  5. text box & buttons on window .. pls help urgent ???
    By intruder in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 PM

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