![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4
![]() |
it is causing problems lol, its telling me that in this code:
//if(i_startOfLine==3 && ch==",") causing problems //cout << "CH->" << ch;
__________________
#if 0 /* in case someone actually tries to compile this */ <Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode? |
|
|
|
|
|
#3 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#4 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
//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);
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
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 |
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#8 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
It's "get", which is an overload basic istream method.
Quote:
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 |
|
|
|
|
|
|
#9 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|