1.11M Members

Multiple FIle Pointers

 
0
 

Algorithm
step1: display 3 choices
step2: user inputs one choice
step3: Choice is store in one variable
step4: selected choice is transfered to switch function
step5: choice is compare to cases
step6: a text document is created stating you choice.

This is layout of the code:

#include<stdio.h>

void main()
{
char chois;
printf("Enter your choise");
scanf("%c",&chois);

switch(chois)
{
    case 1:
    FILE *fp;
        fp=fopen("file1.txt","w");
        fprintf(fp,"%s","You choosed 1");
        fclose(fp);

    case 2:
FILE *fp;
        fp=fopen("file2.txt","w");
        fprintf(fp,"%s","You choose 2");
        fclose(fp);
    break;

    case 3:
FILE *fp;
        fp=fopen("file3.txt","w");
        fprintf(fp,"%s","You choose 3");
        fclose(fp);
    break;

    default:
FILE *fp;
        fp=fopen("file4.txt","w");
        fprintf(fp,"%s","Invalid CHooise");
        fclose(fp);
    break;
}

 }
 
2
 

line 12: Do not declare variables in switch statements like that, but declare FILE* at the top of main() so that it is global to the entire function.

 
0
 

Thanx a lot I had made the changes and it is working correctly

#include<stdio.h>

void main()
{
int a;
printf("Enter your choise ");
scanf("%d",&a);
FILE *fp;
switch(a)
{
    case 1:
    printf("printing");
        fp=fopen("file1.txt","w");
        fprintf(fp,"%s","chois 1");
        fclose(fp);
printf("successfully");
    break;
    case 2:
    printf("printing");
fp=fopen("file2.txt","w");
        fprintf(fp,"%s","chois 2");
        fclose(fp);
        printf("successfully");
    break;
    case 3:
    printf("printing");
fp=fopen("file3.txt","w");
        fprintf(fp,"%s","chois 3");
        fclose(fp);
        printf("successfully");
    break;
    default:
    printf("printing");
fp=fopen("file4.txt","w");
        fprintf(fp,"%s","invalid chois 1");
        fclose(fp);
        printf("successfully");
    break;
}


 }
 
0
 

Sir Please check this code .Is it correct or is there any way to make it still small

 
0
 

Except for line 3 it seems to meet the requirements of the instructions you originally posted. line 3 should be: int main() because main() always returns an int.

 
0
 

Thanxx alot once agin sir.. :)

 
0
 

Well, to make it smaller, you could do like this:

#include <stdio.h>
#include <string.h>

int main(){
    FILE *f;
    char cho;
    char filename[] = "text .txt";
    char text[] = "Your choice is  .\n";
    char* error,* errtxt;
    printf("Insert choice: ");
    scanf ("%c", &cho);
    switch(cho){
    case '1': filename[4] = text[15] = cho; break;
    case '2': filename[4] = text[15] = cho; break;
    case '3': filename[4] = text[15] = cho; break;
    default:
        error = "error.txt";
        errtxt = "Invalid choice.\n";
        strcpy(filename, error);
        strcpy(text, errtxt);
        free (error); free (errtxt);
    break;
    }
    f = fopen(filename, "w");
    fputs(text, f);
    fclose(f);
    return 0;
}
 
0
 

It is givning an Error "Call to undefined function'free' in function main()"

 
1
 

just delete line 21, not needed since those pointers were not allocated by calls to malloc()

 
0
 

Great sir, Hats off.....
It worked

Question Answered as of 1 Year Ago by Ancient Dragon and Lucaci Andrew
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: