User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,924 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,696 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 237 | Replies: 3 | Solved
Reply
Join Date: Jul 2008
Posts: 3
Reputation: Gromnie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gromnie Gromnie is offline Offline
Newbie Poster

TXT File Loading Prog

  #1  
32 Days Ago
my 2nd prog, i intend on adding to it next so that it manipulates the data, see what cool things i can make it do....anyways, just looking for some feedback again on my prog, does it suck ? Thx in advance
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
class WordRead
{
public:
	WordRead();
	~WordRead();
	void EnterName();
	void LoadFile();


private:
	char ch;
	char theFile[1000];
	ifstream myFile;
	vector<char> load;
};

WordRead::WordRead()
{
}

WordRead::~WordRead()
{
}

void WordRead::EnterName()
{
	cout << "Type in the text file path you wish to read: ";
	cin >> theFile;
}

void WordRead::LoadFile()
{
	myFile.open(theFile);
	for(int i = 0; !myFile.eof(); i++)
	{
		myFile.get(ch);
		load.push_back(ch);
		cout << endl << "char " << i << ": " << load[i];
	}
	myFile.close();

}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: TXT File Loading Prog

  #2  
32 Days Ago
The only two things I see that are guaranteed problems are first in EnterName(). You should input text using getline().
  1. void WordRead::EnterName()
  2. {
  3. cout << "Type in the text file path you wish to read: ";
  4. cin.getline( theFile, 1000 );
  5. }
And second in LoadFile(), your loop will never terminate if you never hit EOF (which can happen if a read error occurs). A better idiom is to terminate if the istream indicates any failure.
  1. for(int i = 0; myFile.good(); i++)
But you've also got another problem. Even if you hit EOF, you still try to add a character to the end of your vector. As an added note, using the class variable ch is much slower than using a local char variable. Below I suggest something better for you.


You should really consider making use of the STL string library. That will make things much easier, and you'll get better performance than a vector of char.

I think you need to work on your variable names also... Variables should be nouns, and functions should be verbs. Both should be descriptive of what it represents.
  1. ...
  2. #include <string>
  3.  
  4. ...
  5.  
  6. private:
  7. // This is more descriptive: it tells you it is the
  8. // file's -name-, and not some other thing.
  9. string theFileName;
  10. // Fine
  11. ifstream myFile;
  12. // Again, this tells you that it is the file's text.
  13. // 'load' is a verb, and is doubly confusing when
  14. // used anywhere other than in LoadFile().
  15. // (It is confusing there too, though..)
  16. string theFilesText;
  17. ...
  18.  
  19. void WordRead::EnterName()
  20. {
  21. cout << "Type in the text file path you wish to read: ";
  22. getline( cin, theFileName );
  23. }
  24.  
  25. void WordRead::LoadFile()
  26. {
  27. char c;
  28. myFile.open(theFileName);
  29. for (streamsize i = 0; myFile.get( c ); i++)
  30. {
  31. theFilesText.push_back( c );
  32. cout << "char " << i << << ": " << c << '\n';
  33. }
  34. myFile.close();
  35. }
I use '\n' instead of endl because the second forces a flush every time, while the first only flushes when cout thinks it is a good time. This will make your file load much faster also.

Whew. Nice use of classes, and you seem to have a good idea of structure. Hope this helps.
Reply With Quote  
Join Date: Jul 2008
Posts: 3
Reputation: Gromnie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Gromnie Gromnie is offline Offline
Newbie Poster

Re: TXT File Loading Prog

  #3  
31 Days Ago
I made the changes suggested but ran into a couple problems. This is the prototype of the fstream.open function.

void open(const char *filename, openmode mode = in | out);

so it will not recieve it as a string unless i change it to....

myFile.open(theFileName.c_str());

if i understand this right, that just changes it back into chars? would it be worth it to use that even or just have it as a char in the first place....or is there something i'm missing, another way to use it as a string?

then we have this...

string theFilesText;
char c;
for (streamsize i = 0; myFile.get( c ); i++)	
{		
theFilesText.push_back( c );		
cout << "char " << i << << ": " << c << '\n';	
}

that also has issues with switching from a string to a char...haven't figured out how to fix it yet, i'd assume it would have something to do with using a template, haven't messed with those really yet.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: TXT File Loading Prog

  #4  
31 Days Ago
That's not a problem. Underneath a std::string or a std::vector is just an array anyway, except that all the nasty details of managing it are taken care of for you.

So, yes, use c_str() to get a const char* back that you can use with functions that take them. If your STL is implemented "properly" (IMO) then it shouldn't cost you anything to do it that way.

It looks like the second problem is that I accidentally typed << twice in a row. You can't have two binary operators in a row without an interleaving argument. It has nothing to do with strings. So, change line 32 to:
  1. cout << "char " << i << ": " << c << '\n';
Sorry about that!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:18 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC