Question

Random accesses to a file. A file contains a formatted list of 9999 integers that are...

Random accesses to a file. A file contains a formatted list of 9999 integers that are randomly generated in the range of [1,9999]. Each integer occupies one single line and takes 4 characters' space per line. Alternatively, you can think that each number takes 5 characters' space, four for the number and one for the newline character. Write a C++ program using the seekg() and seekp() functions to insert the numbers 7777 through 7781 between the 6000-th and 6001-st numbers of the input file. Below are a few useful tips:

The tellg() and tellp() functions return the position of the current character in an input stream or output stream, respectively.

You are strongly recommended to use the tellg() function to first learn about the starting position of each integer. This will help you locate the exact starting position to insert the new numbers.

You can use the width() function to specify the number of characters you'd like an integer to occupy.

In addition to the "output" operator (<<), you can also use the write() function to write to a file.

Before you insert the numbers, you will need to first store all the numbers from the 6001-st number in an internal data structure, e.g., array. Otherwise, some of them will be overwritten.

Finally, always call the clear() function before calling the seekg() or seekp() function. Otherwise, you might encounter inexplicable behaviors.

Requirements:

You are not allowed to create or use any other files except the single input file.

You must use seekg() or seekp() to directly identify the insertion point to insert the new numbers.

It's acceptable to hardcode the input file name and implement everything in the main() function. However, it's preferred to create a separate function that handles the insertion of new numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
 
using namespace std;
 
int main()
{
               
         <int> numberVector;
               int createArray[10000]; /*array of 10000 numbers */
               const int SKIP_NUMBER = 6000;//This is the number in file to skip to
               int numberofBytes = 5;//This is the number of bytes to be read in(4byte num + newline)
               int productnew = (numberofBytes * SKIP_NUMBER);// This is meant to be used with seekg()
               fstream file("numbers.txt");
 
 
               if (!file)
               {
                               cout << "This file cannot be opened\n";
                               exit(1); //exit program
               }
               //cout << "check";
               /*file.seekg(productnew);//This will helps us to skip to the pointer to start adding new nums
 
               while (file.is_open())//Thia is to read in the numbers of the file
               {
                               int nextNumber;
                               file >> nextNumber;
                               if (file.fail())
                               {
                                              break;
                               }
                               numberVector.push_back(nextNumber);// The given subscript is out of range  here
               }
.
               file.clear();//This will clear the file handles
 
               file.seekp(productnew);
 
               for (int i = 7777; i <= 7781; i++) //This will help us to insert 5 new numbers starting at pos. 6001
               {
                               file << i << endl;
               }
 
               for (int i = 6006; i <= 9999; i++) //            This will help us to continue to add the rest of the numbers from the file
               {
                               file << numberVector[i] << endl;
               }
               
               int countnum = 0;
 
               while (!file.fail())
               {
                               int number;
                               file >> number;
                               if (number == SKIP_NUMBER)
                               {
                                              break;
                               }
               }
               int numLoc = file.tellg();
               //cout << "To check ";
               file >> createArray[countnum];//The subscript is out of range
               //cout << "to check once";
               while(!file.fail())
               {
                               count++;
                               file >> createArray[countnum];
               }
               //cout << "to check twice";
               file.clear();
               file.seekp(productnew);
 
               for (int i = 7777; i <= 7781; i++)
               {
                               file << i << endl;
               }
               //cout << "check thrice";
               for (int i = 0; i < count; i++)
               {
                               file << createArray[i] << endl;
               }
               //cout << "final check";
 
               cout << "The numbers are getting placed in a proper order";
               
               
               file.close();
 
               return 0;
}


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
Random accesses to a file. A file contains a formatted list of 9999 integers that are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • c++ language Write a program that contains the following functions, i. A function isVowel that takes...

    c++ language Write a program that contains the following functions, i. A function isVowel that takes in a character and returns true if it is a vowel and false otherwise ii. A function that request from the user to enter in a sequence of characters, and outputs the number of vowels entered. (Use Function in a) to assist you, a sentinel value can be used to specify the end of the user input iii. A function that reads 3 test...

  • Write a function called char_counter that counts the number of a certain character in a text file. The function takes tw...

    Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

  • You have to use a h file for class definitions and two (as specified below) cpp...

    You have to use a h file for class definitions and two (as specified below) cpp file for the C++ implementation. You have to generate the list from scratch and check for overflow I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. Please submit enough screenshots showing compiling and execution under Linux. Write a main program...

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT