Friday, 15 June 2012

Numbering Lines in a File With C++ -



Numbering Lines in a File With C++ -

i wrote quick c++ programme asks user input text file , output text file. programme supposed number lines in file on left margin. however, cannot seem working properly, compiles fine not number lines supposed to. believe logical error on part. not familiar file i/o in c++ learning using old school textbooks.

here file:

#include <iostream> #include <fstream> #include <string> #include <cassert> #include <cstdio> using namespace std; int main(void) {int = 0 , num = 1; string inputfilename; string outputfilename; string s; ifstream filein; ofstream fileout; char ch; cout<<"enter name of input file: "; cin>>inputfilename; cout<<"enter name of output file: "; cin>>outputfilename; filein.open(inputfilename.data()); fileout.open(outputfilename.data()); assert(filein.is_open() ); assert(fileout.is_open() ); while (!(filein.eof())) {ch=filein.get(); if (ch=='\n') num++; fileout << num << "\n"; s.insert(i,1,ch); //insert character @ position i++; } fileout << s; filein.close(); fileout.close(); homecoming 0; }

if point me in thr right direction or give me tips eternally grateful.

int = 0; string line; while (getline(infile, line)) { outfile << (i++) << " " << line << "\n"; }

c++ file-io iostream fstream

No comments:

Post a Comment