What's new

C & C++ Patulong po C Programming - C File Handling

Block_MC

Eternal Poster
Joined
Apr 29, 2018
Posts
351
Solutions
1
Reaction
64
Points
257
Hello mga kuys! Please pa help HAHAHHAHA been debugging this since morning tas di ko alam pano eh.
Pano ba mag lagay ng file extension na .txt sa C File handling instead of typing it manually as a user?

Bale ang magiging name ng file is depende sa name ng student na ininput as a user
Example: Juan
Tapos yung name naman ng file is magiging Juan.txt dapat.
1651941079870.png


Here's my code po.

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE * fptr; int i, n; int id, math, sci, eng, sum = 0, ave = 0; char name[255]; printf("Input how many students: "); scanf("%d", & n); for (i = 0; i < n; i++) { printf("\nEnter student name: "); fgets(name, sizeof name, stdin); scanf("%s", name); fptr = fopen(name, "w"); printf("Student ID Number:"); scanf("%d", &id); printf("Math Grade:"); scanf("%d", &math); printf("Science Grade:"); scanf("%d", &sci); printf("English Grade:"); scanf("%d", &eng); printf("\nData written succesfully!"); sum = math + sci + eng; ave = sum / 3; fprintf(fptr, "Student Name: %s", name); fprintf(fptr, "\nStudent ID number: %d", id); fprintf(fptr, "\nMath Grade: %d", math); fprintf(fptr, "\nScience Grade: %d", sci); fprintf(fptr, "\nEnglish Grade: %d", eng); fprintf(fptr, "\nTotal grade: %d", sum); fprintf(fptr, "\n\nGrade average: %d", ave); } return 0; }
 

Attachments

use this on name input.
Code:
fptr = fopen(strcat(name,".txt"), "w")

also. don't forget to close the file
 
use this on name input.
Code:
fptr = fopen(strcat(name,".txt"), "w")

also. don't forget to close the file
what if walang kasamang .txt sa output?
basically dapat walang .txt sa output pero yung file is automatic declared as FileName.txt

what if walang kasamang .txt sa output?
basically dapat walang .txt sa output pero yung file is automatic declared as FileName.txt
1652002756366.png

Okay na yung File na merong .txt pero pano alisin yung .txt sa output sa Student Name?
 

Attachments

gawa ka ng tmp variable:

Code:
 char name[] = "juan"; //get input
char file[45];
strcpy(file, name); //copy name
strcat(file,".txt"); //concatenate
 
gawa ka ng tmp variable:

Code:
 char name[] = "juan"; //get input
char file[45];
strcpy(file, name); //copy name
strcat(file,".txt"); //concatenate
san po ba ito ilagay kuya? lumalabas parin kasi yung txt AHAHAHH idk kung dama ba pag lagay ko
 
Code:
char name[] = "juan"; //get input
char file[45];
strcpy(file, name); //copy name
strcat(file,".txt"); //concatenate
fptr = fopen(file, "w");
 

Similar threads

Back
Top