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];

cin>>name;
cin>>city;
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

English requires the use of sentences, not one long unreadable statement.
Attachments
From a very early age i was very much into computing most probably that is because i have been growing up in the shadow of the my great brother who also  did his major in the the field  of  computer sciences.He used to lend me books and for a kid being a nerd in computers at little age means a lot of respect with in school so i really picked on this opppurtunity and statred getting my hands on computer related things such that at a very little age i was able to trouble shoot a lot of problems that other people my age werent able to .As an indivisual this gives a great pleasure and satisfaction to me also it gave a future outlook towards my future career planning i mean if i am great in this field then why shouldnt i follow it as whole as a major after all i am doing good till now so i think that this is the real reason why i wanted to pursue this feild since i am currently enrolled in the universty doing computerscience i have the first hand experiance in programming and therefore i am confident that i would be able to clear it out with distinction bieng in this field .
i have learned a lot and  since learning computer is my hobby i have been studing advance web devloupment as a means to make sure that i not only ace in these subjects but also  free lance contributer.

so that once i am in the field i am no longer the new cause i hate to be caught of gaurd or  get a glinch when it comes to my future dreams regarding my self and this is really where USA comes in sight. As you know that degree of USA is tacken with very high regards with in Pakistan and since even mention of spending  a  year   in  usa  or similler country  in scholership is bound to get a good job  so it is very essential that i do not miss this oppurtunity cause otherwise it might take me more time to make my self finaclly stable for going for the graduate level education which i am going to pursue so it would save me a few more months or even years that might be put to even better use
5
Contributors
6
Replies
11
Views
5 Years
Discussion Span
Last Post by thines01

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.

Edited by LRRR: n/a

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?