I mean it, which i wrote in my question because i asked same questions to others but i got answ that they are't able to give me answer.
Ok, i have one more question
firstly i'm using win XP and compiler is TC++
question is i have to read a file. i'm skipping some lines by reading them, after this i'm reading that line which i want and then i'm writeing this line on output file.
here is my code
but it's showing odd output. i'm taking SIZE=1024.
In inputfile few lines are very long n few are too short.
i made a file, only 5 lines r there.
i took experiment on few lines, like i start reading file from bigning and then write it on output file. It writes that line accurately but after that line it's writing another line on same row which does not exist on source file, i do't know why and where from.
can u tell me how can i flush inputstream.
First off, which version of Turbo C++ (I presume this is what you meant by TC++) are you using? The older versions are 16-bit, pre-ISO standard C++ compilers that these days should be considered pretty obsolete (see http://en.wikipedia.org/wiki/Turbo_C++
for a brief history). Note that if you are using one of the old 16-bit Turbo C++ compilers then you cannot make use of any Win32 features, you are stuck in 16-bit compatibility mode of the Windows on Windows (WOW) subsystem. You also would not have access to standard C++ library features. In particular you will be using the Borland implementation of the pre-standard (traditional) IOStreams library, about which I have no, or little, specific information. If you do have an old pre-ISO standard (and 16-bit) compiler, then I really urge you to consider updating to a more up to date implementation. Both Borland and Microsoft have free downloadable C++ offerings, see http://www.thefreecountry.com/compilers/cpp.shtml
for a list of free compilers, including the Microsoft and Borland offerings.
Secondly, your question and shown code do not give enough information to pin point all your problem(s) for sure.
However I would start by reading the small print of the write function. This function does _not_ write C-style strings. If you give it a pointer to a buffer of n chars (or more) and tell it to write n chars then it will write n chars to the best of its ability. It _will_ _not_ take note of any end of string markers because, as I said, it is _not_ intended for writing strings! It is intended to allow the writing of buffers of binary data. Thus you can either tell write to write just the characters from the read string (e.g. by passing the value returned from strlen(line) instead of SIZE to write). This still will not get you an end of line as write does not, of course, add one automatically, and getline will read one if encountered before the buffer is full but not place it into the buffer. Thus a more obvious way to write your data is to use the stream insertion operators (operator<<):
fwrong << line << '\n';
Or you could use std::endl (or just endl, if using a pre-ISO standard library):
fwrong << line << endl;
The point about endl is that it writes the end of line to the stream and flushes the stream (which answers your final question I think). ISO standard C++ output streams also have a flush operation, and a quick look in the pre-ISO standard "The C++ Programming Language" 2nd edition by Bjarne Stroustrup indicates this should be so for pre-ISO standard (traditional) IOStreams also:
Output streams are also flushed before being closed of course. Note that continually flushing data to file will slow down your program. If this becomes noticeable then consider not flushing so often (if doing so often, for safety for example). In most cases I tend to write data to a file in one lump then close it. If the file is to be left open for a long time with sporadic writing, then I might flush after each chunk of output, where the length of the chunk depends on the circumstances. In really critical cases I might open the file, write a chunk of data and close it again (an error log file for example).
Note also that if any of your input lines exceed 1023 characters (standard behaviour, check your compiler library documentation for non-standard behaviour) then getline will not read the whole line. This would lead to such lines being written across more than one line unless you take additional steps.
If you do have a compiler that has an ISO standard C++ library then you can read from a stream into a std::string class object (include <string>):
This should only fail for very, very long lines that exhaust available memory for the line std::string object to use to store the characters in (unless some other problem is encountered of course). The exact reasons for the above getline function to stop reading and storing characters are:
- the firstfile.width() is greater than 0 and width() characters are stored
- firstfile.good() is false (indicating a stream error and may raise an appropriate exception if the stream is so set up)
- a delimiting end-of-line ('\n') is extracted (but not stored)
- line.max_size() characters are stored
Hope this is of help.