Question

In: Computer Science

Write a C ++ program which opens a file and reads several numbers, utilizing the fscanf()...

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?

Solutions

Expert Solution

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 |


Related Solutions

C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should...
Write a C program that opens a file called "numbers.txt" in writing mode. The program should then read floating point numbers from the keyboard, and write these lines to the opened file one per line, stopping when the number 0 is entered. Your program should check to make sure that the file was opened successfully, and terminate if it was not.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT