guys i have a little problem in my file handling program that is i cant get the whole file to be saved just the end portion i want to know why this happens and how to counter it

// file handling flag testing and practise.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
 fstream infile("D:\\muqeet.txt");
char name [10000];
int experience;
char city [10000];

/*infile>>"Enter Your Name(Without Space):";
cin>>name;
cout<<"Enter Your City(Without Space):";
cin>>city;
cout<<"Enter Your Experience(Without Space):";
cin>>experience;*/
while(!infile.eof())
{infile.getline(city,10000);
;}
cout<<city;
system("pause");
return 0;
}

one more thing that is if i place the cout with in the while loop it shows every word this is curious can any one explain how this happens

WaltP commented: English requires the use of sentences, not one long unreadable statement. -4

Recommended Answers

All 6 Replies

It because you didnt include the cout<<city in the while loop so just outputted the end portion.

but i want to perform the operations on all of the text document not the end part but i wont be able to do that if only the lower part is stored on the string so i wanna know how to over come that problem thanks

Its very clear what your problem is ! You are opening the file using object inline without describing any mode... You have used fstream inline.
If you would like to use the file to input data into the file use ofstream inline and if you would like to view data from the file use ifstream inline !!

I just want to remind you for a few thing:
Getline only reads until the first delimitation character is found which is by default is '\n' the new line character.

Keep that in mind, that you can't read 10000 characters in a c-style string, there are only 9999 available space, the 10000th is for the character '\0'.

Another thing, by looping through with getline, you are keep overwriting you array.
And don't use system("pause"), it is bad practice. Use cin.get() instead, or getchar if you are programming in C.

Agree with LRRR !! What the hell dya require 10000 for ?? Since wen did names get so big? And u do realise you just wasted a load of ram space.. Reserving 10000 for city and name - The char size depends from system to system and is a minimum of 1 byte.. So u just wasted 2 x 10000 = 20,000 bytes or close to 19Kb for nothing...

Coming to ur problem :
Its still that u havent defined the stream...Use any of the following :

ofstream infile("D:\\muqeet.txt");
 
           //OR

fstream infile;
infile.open("D:\\mugeet.txt",ios::out);

Are you redirecting the content out to a file or did you want to create an output file in code and write data to it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.