By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

filing struct fields from input file

P: n/a
I have decided to use a struct to collect my data. The input file is
comma dilineated between almost all of the fields. Here is the code I
have so far and a sample input and output file.

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

struct pyrll
{
int id;
string job;
char bulddep;
string pstn;
char lttr; //can I set this as a constant where it is just a
letter L
string pygrp;
float ttlhrs;
};

pyrll employee;

#define inFile "C:\\timeforce.txt"
#define outFile "C:\\timberline.txt"

int main()
{
ifstream ins;
ofstream outs;

ins.open(inFile);
if(ins.fail())
{
cerr << "*** ERROR: Cannot open " << inFile
<< " for input." << endl;
return EXIT_FAILURE;
}

outs.open(outFile);
if (outs.fail())
{
cerr << "*** ERROR: Cannot open " << outFile
<< " for output." << endl;
return EXIT_FAILURE;
}
cout << "Input file copied to output file." << endl;

ins.close();
outs.close();

return 0;
}
input sample
540,,,VAC3,8.00
597,2,030-112,32,5.00
657,193-1741R,030-150,11,1.95

ouput sample
540,0002-0000,,,L,VAC3,8
4,0007-0000,,070-042,L,HRLY,39.93
409,767-0312,R,030-151,L,30,3.35

any help would be greatly appreciated I have to gather input from file
and make slight modifications and output to file.

May 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
jwright wrote:
struct pyrll
Try complete words, like payroll. Fix all your indentifiers like that (and
take a keyboarding class if this is onerous).
{
int id;
string job;
char bulddep;
string pstn;
char lttr; //can I set this as a constant where it is just a letter L
Yes; you don't mean constant, you just mean an 'L' value in single quotes.
string pygrp;
float ttlhrs;
};

pyrll employee;

#define inFile "C:\\timeforce.txt"
#define outFile "C:\\timberline.txt"
Try static string inFile = "C:/timeforce.txt";

Your version abuses #define, which even the newbies here will warn you
against.
any help would be greatly appreciated I have to gather input from file
and make slight modifications and output to file.


ins >> employee.id next, right?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
May 3 '06 #2

P: n/a
what do you mean "almost all fields"?
you have to know the exact format of the input file the read
accordingly.

what exactly your problem? in your you are not reading form the file at
all.

May 3 '06 #3

P: n/a
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

struct payroll
{
int id;
string job;
char builddept;
string position;
char letter;
string paygroup;
float totalhours;
};

payroll employee;
const char lttr = 'L';

inFile = "C:/timeforce.txt"
outFile = "C:/timberline.txt"

int main()
{
ifstream ins;
ofstream outs;

ins.open(inFile);
if(ins.fail())
{
cerr << "*** ERROR: Cannot open " << inFile
<< " for input." << endl;
return EXIT_FAILURE;
}

outs.open(outFile);
if (outs.fail())
{
cerr << "*** ERROR: Cannot open " << outFile
<< " for output." << endl;
return EXIT_FAILURE;
}

ins >> employee.id;
ins >> employee.job;
ins >> employee.builddept;
ins >> employee.position;
ins >> employee.paygroup;
ins >> employee.totalhours;
cout <<employee.id;
cout <<employee.job;
cout <<employee.builddept;
cout <<employee.position;
cout <<employee.letter;
cout <<employee.paygroup;
cout <<employee.totalhours;

cout <<"Input file copied to output file." << endl;

ins.close();
outs.close();
return 0;
}
Hey Phlip
Since you seem to have quite a bit more experience in programming than
I do I will come to you again for help. I have made the revision you
suggested and here are the errors I am getting if you could offer any
more advice I would greatly appreciate it.

Compiling...
FP1.cpp
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(29) : error C2501: 'inFile' : missing
storage-class or type specifiers
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(30) : error C2440: 'initializing' :
cannot convert from 'char [17]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(30) : error C2146: syntax error : missing
';' before identifier 'outFile'
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(30) : error C2501: 'outFile' : missing
storage-class or type specifiers
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(32) : error C2440: 'initializing' :
cannot convert from 'char [18]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(32) : error C2144: syntax error : missing
';' before type 'int'
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(37) : error C2664: 'void __thiscall
std::basic_ifstream<char,struct std::char_traits<char> >::open(const
char *,int)' : cannot convert parameter 1 from 'int' to 'const char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(45) : error C2664: 'void __thiscall
std::basic_ofstream<char,struct std::char_traits<char> >::open(const
char *,int)' : cannot convert parameter 1 from 'int' to 'const char *'
Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(54) : error C2679: binary '>>' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(56) : error C2679: binary '>>' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(57) : error C2679: binary '>>' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(60) : error C2679: binary '<<' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(62) : error C2679: binary '<<' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
c:\documents and settings\administrator\my
documents\programming\fp1.cpp(64) : error C2679: binary '<<' : no
operator defined which takes a right-hand operand of type 'class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<c
har> >' (or there is no acceptable conversion)
Error executing cl.exe.

FP1.exe - 14 error(s), 0 warning(s)

May 8 '06 #4

P: n/a
jw*****@componentsys.com wrote:
#include <cstdlib>
#include <fstream>
#include <iostream>
# include <string>
#include <iomanip>
Not necessary.
using namespace std;
http://www.parashift.com/c++-faq-lit....html#faq-27.5
struct payroll
{
int id;
string job;
char builddept;
string position;
char letter;
string paygroup;
float totalhours;
};

payroll employee;
Put this in main().
const char lttr = 'L';
What's that?
inFile = "C:/timeforce.txt"
outFile = "C:/timberline.txt"
These have no types?

const std::string inFile = "C:/timeforce.txt";
const std::string outFile = "C:/timberline.txt";
int main()
{
ifstream ins;
ofstream outs;

ins.open(inFile);
Make that

std::ifstream ins(inFile.c_str());

1) use constructors
2) std::ifstream takes a const char*, not a std::string
if(ins.fail())
{
cerr << "*** ERROR: Cannot open " << inFile
<< " for input." << endl;
return EXIT_FAILURE;
}

outs.open(outFile);
Make that

std::ifstream outs(outFile);
if (outs.fail())
{
cerr << "*** ERROR: Cannot open " << outFile
<< " for output." << endl;
return EXIT_FAILURE;
}

ins.close();
outs.close();


Not necessary.
Jonathan

May 8 '06 #5

P: n/a
Thank you all for the help. It has been a great help, now I am going to
refine the code to cleanup my output file, make my revisions, and put
the L in the struct.
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct payroll
{
int id;
string job;
char builddept;
string position;
char letter;
string paygroup;
float totalhours;
};

payroll employee;

const std::string inFile = "C:/timeforce.txt";
const std::string outFile = "C:/timberline.txt";

int main()
{
const char letter = 'L';

std::ifstream ins(inFile.c_str());

if(ins.fail())
{
cerr << "*** Error: Cannot Open " << inFile << " for input." <<endl;
return 0;
}
std::ofstream outs(outFile.c_str());

if(outs.fail())
{
cerr << "*** Error: Cannot Open " << outFile << " for output."
<<endl;
return 0;
}

ins >> employee.id;
ins >> employee.job;
ins >> employee.builddept;
ins >> employee.position;
ins >> employee.paygroup;
ins >> employee.totalhours;

outs <<employee.id;
outs <<employee.job;
outs <<employee.builddept;
outs <<employee.position;
employee.letter = 'L';
outs <<employee.letter;
outs <<employee.paygroup;
outs <<employee.totalhours<<endl;

cout <<"Input file copied to output file." << endl;

return 0;
}

May 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.