Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 27th, 2005, 11:06 PM   #1
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
FSTREAM questions

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char ch;
	int  i_startOfLine=0;
	
	ifstream FileIn("70271.txt");
	ofstream FileOut("dump.txt");

	FileOut << "  File Dump for ()" << endl;
	FileOut << "======================" << endl;

	while(!FileIn.eof())
	{
		FileIn.get(ch);
		
		//if(i_startOfLine==3 && ch==",") causing problems
			//cout << "CH->" << ch;
				
		if(ch==(char)10)
		{
			cout << "TEN" << (char)10;
			i_startOfLine=1;
		}
		else
		{
			cout << ch;
			i_startOfLine++;
		}
	}

	return 0;
}

it is causing problems lol, its telling me that in this code:

		//if(i_startOfLine==3 && ch==",") causing problems
			//cout << "CH->" << ch;

that i cannot test like this?

EDIT:

ERRORS -

C:\Documents and Settings\-\Desktop\DataSearch\main.cpp(22) : error C2446: '==' : no conversion from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\-\Desktop\DataSearch\main.cpp(22) : error C2040: '==' : 'int' differs in levels of indirection from 'char [2]'
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 27th, 2005, 11:40 PM   #2
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
it is causing problems lol, its telling me that in this code:

		//if(i_startOfLine==3 && ch==",") causing problems
			//cout << "CH->" << ch;
You're comparing a character to a string here. Try enclosing the comma in single quotes.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Nov 28th, 2005, 12:36 AM   #3
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
lol that should have been obvious to me O_o. It worked Thanks!


next question: making the file name to open variable it irritating!

this is my current code just for BETA.

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
	char ch;
	string s_fileIn;
	int  i_startOfLine=0,i_emptyCount=0,i_currLine=0;
	bool t1=false,t2=false,t3=false;

	cout << "file name to scan?";
	getline(cin,s_fileIn);

	ifstream FileIn("70271.txt");
	ofstream FileOut("dump.txt");

	FileOut << "  File Dump for ()" << endl;
	FileOut << "======================" << endl;

	while(!FileIn.eof())
	{
		FileIn.get(ch);
		
		if(i_startOfLine==3 && ch==',')
			t1=true;
		if(i_startOfLine==6 && t1==true && ch==',')
			t2=true;
		if(i_startOfLine==9 && t2==true && ch==',')
			t3=true;

		if(t3==true)
		{
			FileOut << "Line(" << (i_currLine+1) << ")=empty record" << endl;
			i_emptyCount++;
			t1=false;t2=false;t3=false;
		}
				
		if(ch==(char)10)
		{
			i_currLine++;
			i_startOfLine=1;
		}
		else
		{
			i_startOfLine++;
		}
	}

	return 0;
}

EDIT:
i cannot remember the process to taking user input and storing it in a string
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 28th, 2005, 2:02 AM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
UPDATE:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	char c_inLine[2000];
	const string s_replaceString="\"DO NOT MAIL\",\"DO NOT MAIL\",\"DO NOT MAIL\",\"\",\"\"";
	string s_fileIn;
	int  i_startOfLine=0,i_emptyCount=0,i_currLine=0;
	bool t1=false,t2=false,t3=false;

	//cout << "file name to scan?";
	//getline(cin,s_fileIn);

	ifstream FileIn("70271.txt");
	ofstream FileOut("dump.txt");
	ofstream FileInfo("ds_info.txt");

	if(FileIn.fail())
	{
		cout << "Opening input file failed." << endl;
		goto done;
	}

	
	if(FileOut.fail())
	{
		cout << "Opening output file failed." << endl;
		goto done;
	}
	
	if(FileInfo.fail())
	{
		cout << "Opening info dump file failed." << endl;
		goto done;
	}

	FileInfo << "+-------------------------------------------------+" << endl;
	FileInfo << "  Info Dump for ()" << endl;
	FileInfo << "+-------------------------------------------------+" << endl;

	while(!FileIn.eof())
	{
	
		FileIn.getline(c_inLine,2000,'\n');
		
		if(c_inLine[0]=='\"' && c_inLine[2]==',' && c_inLine[5]==',' && c_inLine[8]==',')
		{
			FileOut << s_replaceString << (char)10;
			i_emptyCount++;
			goto finish;
		}
		
		//if(strlen(c_inLine)>1)
			FileOut << c_inLine << (char)10;
		//else
			cout << "BLANK LINE!" << endl;
		
		finish:
		
		i_currLine++;
	}

	done:

	FileInfo << "     Line Count: " << (i_currLine-1) << endl;
	FileInfo << "  Empty Records: " << i_emptyCount << endl;
	FileInfo << "  Records Fixed: " << endl;
	FileInfo << "+-------------------------------------------------+" << endl;

	FileInfo.close();
	FileOut.close();
	FileIn.close();

	cout << "Successful!" << endl;
	cout << "Created By Kyle N." << endl;

	system("ds_info.txt");

	return 0;
}

Please comment on my code if you see anything i do without reason... or in a harder way thanks!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 28th, 2005, 5:20 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
//cout << "file name to scan?";
//getline(cin,s_fileIn);
ifstream FileIn("70271.txt");

Can be:

string s_fileIn;
	cout << "file name to scan?" << endl;
	cin >> s_fileIn;
	//cout << "You entered: " << s_fileIn << endl;
	ifstream FileIn(s_fileIn);
You should add error checking.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 28th, 2005, 9:05 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
cin >> s_fileIn;
This is problematic advice. One might define s_fileIn to be a megabyte and reduce the probability of a crash, but it's better just to use an input method that constrains the number of characters that will be allowed. Something like "cin.get (inputBuffer, bufferSize);" would do nicely.

If you don't use error checking before you've learned about it, you're just new. If you don't use it AFTER you've learned about it, you're either lazy, a schlock coder, or just recalcitrant. Not a pretty picture, but there you have it .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 28th, 2005, 9:24 AM   #7
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
is that getline DaWei or just get???
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 28th, 2005, 9:52 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's "get", which is an overload basic istream method.
Quote:
Originally Posted by My compiler docs
The fourth function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores char_type after any extracted elements it stores. In order of testing, extraction stops:

At end of file.


After the function extracts an element that compares equal to _Delim, in which case the element is put back to the controlled sequence.


After the function extracts _Count - 1 elements.


If the function extracts no elements, it calls setstate(failbit). In any case, it returns *this.
"Delim" defaults to a newline, but one may specify another (but not a set).

There is a basic istream getline and there is a string class getline. I often use the latter.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 28th, 2005, 1:10 PM   #9
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
so i can use only failbit for error handling? i do not need .good .bad?
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 28th, 2005, 1:48 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There are a set of failure bits. You do not need to manipulate them directly. If .good returns false, you have a problem (request didn't return what you were expecting), but it may or may not be serious. It may just be EOF. You can check for that specifically, if you like, with .eof (). .fail () means a failure, not necessarily fatal, like a conversion didn't work or you hit EOF or something. .bad represents a serious error, you probably should just abort (although you could clear the failures and try again). You can clear the failure indications with .clear (). This doesn't remove content from the stream. If content is causing the failure (alphabetic data that won't convert to a number, for instance), then you have to get it out of there by one of a number of means (.sync, .ignore, reading stuff until it's gone, whatever).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC