Write a C (Not C++) program Using Visual Studio 2015. A. Write a program to read
ID: 3780370 • Letter: W
Question
Write a C (Not C++) program Using Visual Studio 2015.
A. Write a program to read the name and marks of n number of students from user and store them in a file. if the file exists, add the information of n students.
Input:
Enter number of students: 2
For student 1
Enter name: A
Enter marks:100
For student 2
Enter name: B
Enter marks: 200
B. Write a program that converts all letters in a file to upper case. (Characters other than letters shouldn't be changed.) The program should obtain the file name from the command line and write its output to stdout.
Explanation / Answer
//Tested on Ubuntu,Linux
/************************fileCreation.c***************/
#include <stdio.h>
int main() {
//variable declarations
FILE *file;
char fileName[50];
char name[50];
int n,i,mark;
//prompt for file name from user
printf("Please Enter file name ");
scanf("%s",fileName);
//opening file in reading mode
file = fopen(fileName, "w+");
if(file == NULL) //if file does not exist, create it
{
printf("File Does Not exist ");
return 1;
}else {
//prompt for number of student
printf("Enter number of students:");
scanf("%d",&n);
//writing file header into file
fprintf(file, "Name Mark ");
// loop for student info
for(i=0;i<n;i++) {
printf("For student %d ",(i+1));
printf("Enter name:");
scanf(" %[^ ]s", name);
printf("Enter marks:");
scanf("%d",&mark);
// writing student info into file
fprintf(file, "%s %d ", name,mark);
}
// closing file
fclose(file);
}
return 0;
}
/****************output***************/
raj@raj:~/Desktop/chegg$ gcc fileCreation.c
raj@raj:~/Desktop/chegg$ ./a.out
Please Enter file name
test.txt
Enter number of students:2
For student 1
Enter name:A
Enter marks:200
For student 2
Enter name:B
Enter marks:300
/*************test.txt content***************/
Name Mark
A 200
B 300
/**********************smallToCapital.c********************/
#include <stdio.h>
#include <ctype.h>
int main() {
//variable declarations
FILE *file;
char fileName[50];
int c;
char ctr;
//prompt for file name from user
printf("Please Enter file name ");
scanf("%s",fileName);
//opening file in reading mode
file = fopen(fileName, "r");
if(file == NULL) //if file does not exist, create it
{
printf("File Does Not exist ");
return 1;
}else {
//reading file character by character
while((c=fgetc(file))!=EOF) {
ctr= (char) c;
// if ctr is alphabet then we will convert it in to upper case
//then we printing it to upper case
if(isalpha(ctr)){
ctr=toupper(ctr);
printf("%c",ctr);
} else {
printf("%c",ctr);
}
}
printf(" ");
// closing file
fclose(file);
}
return 0;
}
/**********************temp.txt content**************/
Hey Guys How are you?
I am GoOd.
I got 2000 Note.
/*********************output***************/
raj@raj:~/Desktop/chegg$ gcc smallToCapital.c
raj@raj:~/Desktop/chegg$ ./a.out
Please Enter file name
temp.txt
HEY GUYS HOW ARE YOU?
I AM GOOD.
I GOT 2000 NOTE.
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.