Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file into a structure

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Given a text file of:-
C123450000015000
C223450002134111
C012353302343422
C333450003245436

.. and a structure of:-

struct C {
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[3];
};

How do I go about putting each line of the text file into the fields of the structure?

I know I require;
code
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h.

struct C {
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[3];
};

int main()
{
ifstream infile;
infile(&quot;test.dat&quot;, ios::in || nocreate);
if(!infile.is_open())
{
cout << &quot;Could not open file&quot; << endl;
exit(1);
}

char temp[25];
while(!infile.eof())
infile.getline(temp, 25);/code

But after that I am completely confused! Please help if you can.
 
Hi

I don't have any documentation or a compiler at the moment so the following is done from memory.

The fread function can be used to read a data file into a structure. The following code sample gives a basic demonstration of how it works. I can't remember how to detect the end of file. I have used '!fPtr.EOF'. If it doesnt work then just post the error messages.

The fread function returns the number of bytes read which you should really check is the correct amount. This could be a way of detecting the EOF.

Good luck



#define MAX_RECORDS 200

.
.
.

FILE *fPtr;
C struct_c[MAX_RECORDS];

// open the file
// if you are using a binary file use &quot;rb&quot; instead of &quot;r&quot;
fPtr = fopen(&quot;test.dat&quot;, &quot;r&quot;);
if(fPtr == NULL)
{
cout << &quot;Error opening file\n&quot;;
exit(1);
}

// read each of the records
// fPtr.EOF may not detect the eof correctly
for(int i=0; (i < MAX_RECORDS) && (!fPtr.EOF); i++)
{
fread(struct_c, 1, sizeof(C), fPtr);
}

fclose(fPtr);
 
What does it mean?:
ifstream infile;
infile(&quot;test.dat&quot;, ios::in || nocreate);
The parameter ios::in||nocreate is not required for ifstreams. You can't create a file by opening it in read mode.
You can do or
ifstream infile(&quot;test.dat&quot;);
or
ifstream infile;
infile.open(&quot;test.dat&quot;);
C oneInstanceOfStruct;
To read file is very simple. Do you remember cin>>x? With ifstreams is the same
infile>>oneInstanceOfStruct.RecordType;
infile>>oneInstanceOfStruct.Customer_Code;
infile>>oneInstanceOfStruct.Part_Number;
infile>>oneInstanceOfStruct.Issue_Quantity;

If the file's structure is correct, infile will automatically update readposition. John Fill
1c.bmp


ivfmd@mail.md
 
Thank you both for your suggestions. While it is possible I have incorporated your code fragments in the wrong places, both programs did not compile correctly. I have therefore pasted both programs with their relavant compilation errors below:-

1: JohnFill

code
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>

struct C {
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[4];
};

struct C oneInstanceOfStruct;

int main()
{
ifstream infile;
infile.open(&quot;test.dat&quot;);
if(!infile.is_open())
{
cout << &quot;Could not open file&quot; << endl;
exit(1);
}


while(!infile.eof())
{
infile >> oneInstanceOfStruct.Record_Type;
infile >> oneInstanceOfStruct.Customer_Code;
infile >> oneInstanceOfStruct.Part_Number;
infile >> oneInstanceOfStruct.Issue_Quantity;
}
return 0;
}/code

--------------------Configuration: Chapter_6- Win32 Debug--------------------
Compiling...
Chapter_6.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(28) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int [5]' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(29) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int [6]' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(30) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'int [4]' (or there is no acceptable conversion)
Error executing cl.exe.

Chapter_6.obj - 3 error(s), 0 warning(s)

2:-CraigD

code
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#define MAX_RECORDS 200

struct C {
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[4];
};

int main()
{
FILE *fPtr;
C struct c[MAX_RECORDS];

fPtr = fopen(&quot;test.dat&quot;, &quot;r&quot;);

if(fPtr == NULL)
{
cout << &quot;Error opening file\n&quot;;
exit(1);
}

for(int i = 0; (i < MAX_RECORDS) && (!fPtr.EOF()); i++)
{
fread(struct_c, 1, sizeof(C), fPtr);
}

fclose(fptr);

return 0;
}/code

--------------------Configuration: Chapter5_ArrayofPointers - Win32 Debug--------------------
Compiling...
PT39.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(15) : error C2065: 'FILE' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(15) : error C2065: 'fPtr' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(15) : warning C4552: '*' : operator has no effect; expected operator with side-effect
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(16) : error C2236: unexpected 'struct' 'c'
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(16) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(16) : error C2143: syntax error : missing ';' before '['
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(18) : error C2065: 'fopen' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(26) : error C2059: syntax error : '('
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(26) : error C2228: left of '.i' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(26) : error C2143: syntax error : missing ';' before ')'
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter5_ArrayofPointers\PT39.cpp(27) : error C2143: syntax error : missing ';' before '{'
Error executing cl.exe.

PT39.obj - 10 error(s), 1 warning(s)

I am only a student at the moment(I'm sure you would never have guessed!) so I hope you will both be able to set me straight. Thanks your time.
 
You should follow the logic bellow:
////
#include <iostream>
#include<string>
#include<strstream>
#include <fstream>
using namespace std;
struct C
{
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[4];
};

struct C oneInstanceOfStruct;

int main()
{
ifstream infile;
infile.open(&quot;cc.txt&quot;);
if(!infile.is_open())
{
cout << &quot;Could not open file&quot; << endl;
exit(1);
}
while(!infile.eof())
{
infile.read(&oneInstanceOfStruct.Record_Type,1);
}
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
You should follow the logic below:
#include <iostream>
#include<string>
#include<strstream>
#include <fstream>
using namespace std;
struct C
{
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[4];
};

struct C oneInstanceOfStruct;

int main()
{
ifstream infile;
infile.open(&quot;cc.txt&quot;);
if(!infile.is_open())
{
cout << &quot;Could not open file&quot; << endl;
exit(1);
}
while(!infile.eof())
{
infile.read(&oneInstanceOfStruct.Record_Type,1);
}
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
I have only been taught the use of the read() function with binary files rather than text files. However, the error messages are more or less the same. (See below following paragraph for the code and compilation errors).
I have been told that the problem requires that each record is read into an array called temp. From here each field in the structure can be entered via the array 'temp' through the strncpy() function. Is this possible, as I have been trying without success before I came to this forum?

code
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
#include <strstream>
using namespace std;

struct C {
char Record_Type;
int Customer_Code[5];
int Part_Number[6];
int Issue_Quantity[4];
};

struct C oneInstanceOfStruct;

int main()
{
ifstream infile;
infile.open(&quot;test.dat&quot;);
if(!infile.is_open())
{
cout << &quot;Could not open file&quot; << endl;
exit(1);
}


while(!infile.eof())
{
infile.read(&oneInstanceOfStruct.Record_Type, 1);
infile.read(&oneInstanceOfStruct.Customer_Code, 5);
infile.read(&oneInstanceOfStruct.Part_Number, 6);
infile.read(&oneInstanceOfStruct.Issue_Quantity, 4);
}
return 0;
}/code

Compiling...
Chapter_6.cpp
c:\program files\microsoft visual studio\vc98\include\strstream(51) : error C2039: 'seekdir' : is not a member of 'ios'
c:\program files\microsoft visual studio\vc98\include\ios.h(106) : see declaration of 'ios'
c:\program files\microsoft visual studio\vc98\include\strstream(51) : error C2061: syntax error : identifier 'seekdir'
c:\program files\microsoft visual studio\vc98\include\strstream(54) : error C2039: 'openmode' : is not a member of 'ios'
c:\program files\microsoft visual studio\vc98\include\ios.h(106) : see declaration of 'ios'
c:\program files\microsoft visual studio\vc98\include\strstream(54) : error C2061: syntax error : identifier 'openmode'
c:\program files\microsoft visual studio\vc98\include\strstream(91) : error C2629: unexpected 'class std::eek:strstream ('
c:\program files\microsoft visual studio\vc98\include\strstream(91) : error C2238: unexpected token(s) preceding ';'
c:\program files\microsoft visual studio\vc98\include\strstream(109) : error C2629: unexpected 'class std::strstream ('
c:\program files\microsoft visual studio\vc98\include\strstream(109) : error C2238: unexpected token(s) preceding ';'
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(19) : error C2872: 'ifstream' : ambiguous symbol
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(31) : error C2664: 'class istream &__thiscall istream::read(char *,int)' : cannot convert parameter 1 from 'int (*)[5]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(32) : error C2664: 'class istream &__thiscall istream::read(char *,int)' : cannot convert parameter 1 from 'int (*)[6]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Chapter_6\Chapter_6.cpp(33) : error C2664: 'class istream &__thiscall istream::read(char *,int)' : cannot convert parameter 1 from 'int (*)[4]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

Chapter_6.obj - 12 error(s), 0 warning(s)
 
I have only been taught on looking at theese lines of code:
infile.read(&oneInstanceOfStruct.Record_Type, 1);
infile.read(&oneInstanceOfStruct.Customer_Code, 5);
infile.read(&oneInstanceOfStruct.Part_Number, 6);
infile.read(&oneInstanceOfStruct.Issue_Quantity, 4);
Look the correct variant:
infile.read(&oneInstanceOfStruct.Record_Type,1);
infile.read(&oneInstanceOfStruct.Customer_Code[0],4);
infile.read(&oneInstanceOfStruct.Customer_Code[1],4);
infile.read(&oneInstanceOfStruct.Customer_Code[2],4);
infile.read(&oneInstanceOfStruct.Customer_Code[3],4);
infile.read(&oneInstanceOfStruct.Customer_Code[4],4);
.......
//:)
//1. Customer_Code is an array of integers, not an int
//2. do you remember? the size of int is 4 bytes.
My advise is you to be more attentive on types of variables.
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top