In: Computer Science
Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf() function.
Can you add few comments with explanations what is going on?
1.txt
hello 123
#source cod in c++
#include <iostream>
using namespace std;
int main(){
FILE *fileptr =fopen("1.txt","r"); //here open the file in readmode
if(fileptr==NULL){ //if file is not found than it shows this msg
cout<<"The file in not found in the current path!\n";
return 0;
}
char data[100]; //here declare the character array
int value; //here declare the int
fscanf(fileptr,"%s %d",data,&value); //here in the file read string and int
cout<<data<<" "<<value<<" "<<endl; //here print the string and the int value
return 0;
}
#source code in c:
#include <stdio.h>
int main(){
FILE *fileptr =fopen("1.txt","r"); //here open the file in readmode
if(fileptr==NULL){ //if file is not found than it shows this msg
printf("The file in not found in the current path!\n");
return 0;
}
char data[100]; //here declare the character array
int value; //here declare the int
fscanf(fileptr,"%s %d",data,&value); //here in the file read string and int
printf("%s %d\n",data,value); //here print the string and the int value
return 0;
}
#output:
#if you have any doubt or more information needed comment below..i will respond as possible as soon..thanks..
We were unable to transcribe this image
1 2 3 4 5 6 7 8 #include <stdio.h> int main() { FILE *fileptr =fopen("1.txt","r"); //here open the file in readmode if(fileptr==NULL) { //if file is not found than it shows this msg printf("The file in not found in the current path!\n"); return 0; } } 9 10 11 12 13 char data[100]; //here declare the character array int value; //here declare the int fscanf(fileptr,"%s %d", data,&value); //here in the file read string and int printf("%s %d\n", data, value); //here print the string and the int value return 0; ]
user@user-Latitude - 3490:-/Desktops gcc 1.C -0 samp user@user-Latitude - 3490:-/Desktop$ ./samp hello 123 user@user-Latitude - 3490:-/Desktops |