i need to tranform from C to C++

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter6: Using Arrays
Section: Chapter Questions
Problem 18RQ
icon
Related questions
Question

i need to tranform from C to C++

//appends $$$ to a place between minimum element number and maximum element number
//for datafile 1
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
//make a pointer for the file
FILE *fptr;
//make length variable for length of whole datafile, and for where the user wants to truncate
int length, num;
//make an array for the original string, truncate
char string[5000];
//make an array for the data wanting to be appended
char app[3] = {'$', '$', '$'};
//open the file
fptr = fopen("datafile1.txt","r");
//print the string if the file opened successfully and display error message and end program if it isn't
if (fgets(string,5000,fptr) != NULL)
{
printf("The string in the file is\n\n");
puts(string);
}
else
{
printf("The file could not be opened.\n");
exit(1);
}
//close the file
fclose(fptr);
//find the string length
length = strlen(string);
Transcribed Image Text://appends $$$ to a place between minimum element number and maximum element number //for datafile 1 #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { //make a pointer for the file FILE *fptr; //make length variable for length of whole datafile, and for where the user wants to truncate int length, num; //make an array for the original string, truncate char string[5000]; //make an array for the data wanting to be appended char app[3] = {'$', '$', '$'}; //open the file fptr = fopen("datafile1.txt","r"); //print the string if the file opened successfully and display error message and end program if it isn't if (fgets(string,5000,fptr) != NULL) { printf("The string in the file is\n\n"); puts(string); } else { printf("The file could not be opened.\n"); exit(1); } //close the file fclose(fptr); //find the string length length = strlen(string);
//display the length of the string
printf("\n\nThe length of the string is %d characters long.\n", length);
//ask the user where they want to append the data
printf("Where would you like to append the string?\n\n");
//create a whileloop for when there is no place to truncate, once the user inputs a number to num, the loop will exit
while(num<1 || num>length-2)
{
//ask a user to enter where they want to append and scan it into num
printf("Enter a number between one and %d:\n", length-1);
scanf("%d", &num);
}
//open the datafile to read up to the point where the user wanted
fptr = fopen("datafile1.txt","r");
//show error message if the file couldn't be opened
if(fptr = NULL)
{
printf("Unable to open file.\n");
//create an array for the string before the truncation
char string2[5000];
//put the string up to where the user wanted into the new string2 array
fgets(string2,num+1,fptr);
//close the file
fclose(fptr);
//put the app on the end of string2
strcat(string2,app);
//make a variable for num+3
int j=num+3;
//make a loop to put the rest of string into string2
Transcribed Image Text://display the length of the string printf("\n\nThe length of the string is %d characters long.\n", length); //ask the user where they want to append the data printf("Where would you like to append the string?\n\n"); //create a whileloop for when there is no place to truncate, once the user inputs a number to num, the loop will exit while(num<1 || num>length-2) { //ask a user to enter where they want to append and scan it into num printf("Enter a number between one and %d:\n", length-1); scanf("%d", &num); } //open the datafile to read up to the point where the user wanted fptr = fopen("datafile1.txt","r"); //show error message if the file couldn't be opened if(fptr = NULL) { printf("Unable to open file.\n"); //create an array for the string before the truncation char string2[5000]; //put the string up to where the user wanted into the new string2 array fgets(string2,num+1,fptr); //close the file fclose(fptr); //put the app on the end of string2 strcat(string2,app); //make a variable for num+3 int j=num+3; //make a loop to put the rest of string into string2
Expert Solution
steps

Step by step

Solved in 2 steps with 6 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning