🔒 Closed Patulong po C Programming - C File Handling

Status
Not open for further replies.

Block_MC

Honorary Poster
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.webp


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; }
 
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.webp

Okay na yung File na merong .txt pero pano alisin yung .txt sa output sa Student Name?
 
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");
 
Status
Not open for further replies.

About this Thread

  • 6
    Replies
  • 890
    Views
  • 2
    Participants
Last reply from:
Block_MC

Trending Topics

Online now

Members online
971
Guests online
1,369
Total visitors
2,340

Forum statistics

Threads
2,273,370
Posts
28,949,048
Members
1,235,719
Latest member
Wzpprince
Back
Top