473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read/Write Plobrem with C++

stealwings
Hello everyone,
I'm kind of dummy in this thing of programing but I have to do it so. Well I have this problem; I wrote a read and write program, it reads the file well but when it writes is it only writes the last word of the opened file and in a infinite loop, I have close the file though. Plzz can some one help me? My code is like this:
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. #include <cstdlib>
  6. #include <vector>
  7. #include "code.h"
  8. #include "FileManip.h"
  9. #define MAXLENGTH 256
  10. int main(int argc, char* argv[])
  11. {
  12.     char GetWord[MAXLENGTH];
  13.     ifstream OpenFile;
  14.     ofstream WriteFile;
  15.     OpenFile.open("myfile.txt", ios::in);
  16.     if(!OpenFile)
  17.     {
  18.         cerr<<"File not found!"<<endl;
  19.         exit(1);
  20.     }
  21.     OpenFile>>GetWord;
  22.     cout<<GetWord<<endl;
  23.     while (!OpenFile.eof())
  24.     {
  25.         OpenFile>>GetWord;
  26.         cout<<GetWord<<endl;
  27.     }
  28.     OpenFile.close();
  29.     cout<<"End of file reached!"<<endl;
  30.     char *MyString;
  31.     WriteFile.open("myfile.dat", ios::out);
  32.     if(!WriteFile)
  33.     {
  34.         cerr<<"Couldn't create file."<<endl;
  35.         exit(1);
  36.     }
  37.     MyString=GetWord;
  38.     WriteFile<<MyString;
  39.     while(OpenFile)
  40.     {
  41.         WriteFile<<MyString;
  42.     }
  43.     WriteFile.close();
  44.     return 0;
  45. }
thanks in advance!
Feb 7 '07 #1
3 2085
AdrianH
1,251 Expert 1GB
Hi,

I had a bit of trouble trying to get your file to compile. You have a bunch of extraneous includes, and is probably the reason that you got it to compile, although badly.

Given the first include, it looks like you are using MS VC++. Considering that you did not use a ‘using namespace std;’ line, it is either in your other files that you included (which is bad BTW, never have a using namespace in anything but a source file, otherwise you will eventually have strange clashes occurring) or you have got one really old compiler.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. //#include <cstdlib>
  6. //#include <vector>
  7. //#include "code.h"
  8. //#include "FileManip.h"
  9. #include<fstream>
  10. #include<iostream>
  11.  
  12. using namespace std;
  13.  
Commenting out the extraneous header files and inserting in the relevant ones and the using namespace statement, I was able to compile your program. Good news is that reading section works… almost.

Here is a website that has all the C++ standard library documents: http://www.cplusplus.com/reference/iostream/ifstream/

Instead of using the >> operator to fill your array, use the getline function. Try with what you got and then try with the getline function and you will see what I mean. >> to a char array reads in words but you never see the whitespaces.

You shouldn’t need the extra file read and output just before the loop. It will be handled within the loop.

The reason that you were getting the last word repeated in an infinite loop is because by the time you got to that section of the code:
  1. you were outputting the last word you had read in, since by this time you had read in every ‘word’ in the file in your first loop, which was the last word of the file
  2. you were using OpenFile as if it had a Boolean value. Well it is not, even though you can use the ! operator on it, without the ! operator I’m not even sure what it means. You probably mean to use the fail function, and not on OpenFile but on WriteFile.

Remember, you can read from a file and write to another in the same loop. You are already doing that with cout, so why not open your WriteFile after you open your OpenFile and write the line right after you read it as you are doing with cout.


Hope this helps.


Adrian

PS If you see a message from me before this, disregard it. I tried to delete it.
Feb 8 '07 #2
Hi,

I had a bit of trouble trying to get your file to compile. You have a bunch of extraneous includes, and is probably the reason that you got it to compile, although badly.

Given the first include, it looks like you are using MS VC++. Considering that you did not use a ‘using namespace std;’ line, it is either in your other files that you included (which is bad BTW, never have a using namespace in anything but a source file, otherwise you will eventually have strange clashes occurring) or you have got one really old compiler.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. //#include <cstdlib>
  6. //#include <vector>
  7. //#include "code.h"
  8. //#include "FileManip.h"
  9. #include<fstream>
  10. #include<iostream>
  11.  
  12. using namespace std;
  13.  
Commenting out the extraneous header files and inserting in the relevant ones and the using namespace statement, I was able to compile your program. Good news is that reading section works… almost.

Here is a website that has all the C++ standard library documents: http://www.cplusplus.com/reference/iostream/ifstream/

Instead of using the >> operator to fill your array, use the getline function. Try with what you got and then try with the getline function and you will see what I mean. >> to a char array reads in words but you never see the whitespaces.

You shouldn’t need the extra file read and output just before the loop. It will be handled within the loop.

The reason that you were getting the last word repeated in an infinite loop is because by the time you got to that section of the code:
  1. you were outputting the last word you had read in, since by this time you had read in every ‘word’ in the file in your first loop, which was the last word of the file
  2. you were using OpenFile as if it had a Boolean value. Well it is not, even though you can use the ! operator on it, without the ! operator I’m not even sure what it means. You probably mean to use the fail function, and not on OpenFile but on WriteFile.

Remember, you can read from a file and write to another in the same loop. You are already doing that with cout, so why not open your WriteFile after you open your OpenFile and write the line right after you read it as you are doing with cout.


Hope this helps.


Adrian

PS If you see a message from me before this, disregard it. I tried to delete it.
Thx a lot, the strange header you were talking about "FileManip.h" (in which I included fstream, std:: ifstream and std:: ofstream and well <iostream> and using namespace std; as well). and "Code.h" (which is for a table structure, I'n not using yet) are two of the headers I made sorry for forgot to put that detail, and now I understand a little why I get the infinite loop, so I can't separate the loop for GetWord and WriteFile? I was only using cout so I could see that my GetWord function was working properly. So now the answer to my question is that I should put my WriteFile function into the same loop as my GetWord function so they can work properly? Thanks a lot, I will try.
Feb 9 '07 #3
AdrianH
1,251 Expert 1GB
Thx a lot, the strange header you were talking about "FileManip.h" (in which I included fstream, std:: ifstream and std:: ofstream and well <iostream> and using namespace std; as well). and "Code.h" (which is for a table structure, I'n not using yet) are two of the headers I made sorry for forgot to put that detail, and now I understand a little why I get the infinite loop, so I can't separate the loop for GetWord and WriteFile? I was only using cout so I could see that my GetWord function was working properly. So now the answer to my question is that I should put my WriteFile function into the same loop as my GetWord function so they can work properly? Thanks a lot, I will try.
If you want to separate the Reading from the Writing, you are going to have to either read in the entire file into memory (usually not a preferred choice) or use a class to maintain the read stream and write stream. That way you can create an outer loop from the class that does the read function and a write function, but then you have just gone back to putting the read and write calls into the same loop. So yes, put the read and write in the same loop.

Glad to help.


Adrian
Feb 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
8
by: dosworldguy | last post by:
I have been having a very peculiar issue from a long time. I have an application where multiple clients read from a shared set of files. When a record is changed, sometimes the win9x clients...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.