Advertisement
Guest User

1 - text

a guest
Aug 16th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.     string s[255];
  9.     ifstream in("in.txt"); // Opening a file for reading
  10.    
  11.     cout<<"Enter number"<<endl;
  12.     string n;
  13.     cin>>n;
  14.    
  15.     int num = NULL;        
  16.     for (int i = 0; i < n.length(); i++){    
  17.         num = num*10 + n[i] - 0x30;   //Convert the entered number to a number
  18.     }
  19.  
  20.    
  21.     int i = 1;
  22.     while(getline(in,s[i]))// Read the line from the file
  23.         i++;   
  24.  
  25.     in.close(); //Closed the file
  26.    
  27.     if (num < i)
  28.     s[num] = s[num] + " " + n ; //We add the number after the required line
  29.    
  30.     ofstream of("in.txt"); //Opened the file for writing
  31.    
  32.     for ( int j = 1; j < i; j++)
  33.         {
  34.             of<<s[j];   //Writing lines to a file
  35.             of<<endl;
  36.         }
  37.     of.close(); // Close the file
  38.    
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement