|
 |
|
 |
03-01-2005, 11:25 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 4
|
Text file input to an array
Hello,
I'm relatively new to C++ and trying to read a text file and store the lines of the text file in to an array. Each line in the text file is 0x.... format input representing bits. The following code is part of larger program. When I run the exe, it simply hangs. If I use the cin method of inputting in the array, it works.
int a[100];
ifstream infile("in.txt");
while (! infile.eof())
{ infile>>hex>>a[i];
i++;
}
Also in the same program,
SOURCEIP_STR = SOURCEIP_OCT1 + SOURCEIP_OCT2 + SOURCEIP_OCT2 + SOURCEIP_OCT3 + SOURCEIP_OCT4;
gives me errors that "cannot add two pointers".
All the variables above are type integers. SOURCEIP_OCT1 and other variables are derived from performing bitwise shifts on the elements of a[i].
I'm doing something wrong here. Do I have to use character strings? Is it possible to read the contents of the text file into a character array? How would I do that? I've referred to some sources but I'm confused on what to do here. The whole program is on IP packet fragmentation and re-assembly.
Help would be greatly appreciated!
|
|
|
03-02-2005, 01:48 AM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
- Show me part of the file so I know in which format it is.
- As for the other problem (sourceip), I need to know more about the code.
__________________
|
|
|
03-02-2005, 03:07 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 4
|
Text File input to array - File format
Hi
Thanks for the reply. Here is the part of the input file called "in.txt"
0x4500004c
0x3728000b
0x7D11FB02
0x81800444
0x81800427
I'm reading the array elements with instructions similar to the following;
ip=(a[0] & 0xf0000000)>>28
SOURCEIP_OCT1= (a[3] & 0xff000000)>>24
Looking forward to your reply.
|
|
|
03-03-2005, 03:43 AM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I don't see quite the problem. The code below works fine:
Code:
#include <iostream>
#include <fstream>
#include <ios>
using namespace std;
void print_array(unsigned*, unsigned);
int main(int argc, char *argv[])
{
unsigned x[5]={0};
unsigned i(0);
ifstream ifs("in.txt");
//Let's fill the array from file.
while( ifs>>hex>>x[i] )
{
++i;
}
print_array(x, 5);
//Whatever this means :)
unsigned ip = ( (x[0] & 0xf0000000)>>28);
cout<<hex<<ip<<endl;
return 0;
}
void print_array(unsigned* arr, unsigned size)
{
for(unsigned j = 0; j < size; ++j)
{
cout<<hex<<arr[j]<<endl;
}
}
__________________
|
|
|
03-03-2005, 07:57 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 4
|
Text file input to an array
I understand most of the code but just have some questions:
QUESTION1:If the input file "in.txt" has variable number of lines (50, 55, any number..) and we don't want to specify the size of array at run time or compile time, What methods could we use? Could we use vectors?
QUESTION2: If the input file has two blocks of data and the code is supposed to stop after reading the first block of data into the array, process it and then read the next block of data into the array. How do we implement that? There is one empty line between the two successive blocks of data.
An example of modified code would help me to understand better.
The input file is as below:
1.854
0x4500004c
0x3728000b
0x7D11FB02
0x81800444
0x81800427
0x4500004c
0x3728000b
0x7D11FB02
0x81800444
0x81800427
2.86
0x4500004c
0x3728000b
0x7D11FB02
0x81800444
0x81800427
0x4500004c
0x3728000b
0x7D11FB02
0x81800444
0x81800427
This discussion is most helpful. I've been banging my head again various sources earlier..I'm glad I came here. Looking forward to your reply!
|
|
|
03-04-2005, 12:15 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Try it, and see what happens 
__________________
|
|
|
03-04-2005, 03:45 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
|
Quote:
|
Originally Posted by letsC
Could we use vectors?
|
Try it, and se what happens.
Quote:
|
Originally Posted by letsC
If the input file has two blocks of data and the code is supposed to stop after reading the first block of data into the array, process it and then read the next block of data into the array. How do we implement that? There is one empty line between the two successive blocks of data.
|
Add a condition during the read process checking for your uniq delimiter, then take what ever action you think is propper when reaching it.
|
|
|
03-04-2005, 08:35 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 4
|
Text file input to a Vector
Hi
Thanks for the reply. Here's my attempt to use vectors. It doesn't work. So, there must be something wrong.
Code:
#include <iostream>
#include <fstream>
#include <ios>
using namespace std;
int ip;
float ARR_TIME;
void frag_reassembly (unsigned* a, int, int);
vector<unsigned> a;
ifstream infile("in.txt");
infile>>dec>>ARR_TIME;
infile>>dec>>next;
/* In my opinion, the following should stop reading the file when an empty line is encountered. NULL = empty line*/
while (next != NULL)
{
a.push_back(next);
infile>>hex>>next;
}
cout << " The first data block is: \n";
for (i =0; i < a.size(); i++)
{
cout << a[i] << endl;
}
cout<<"[IP Analysis]\n";
/// Analysis of the 2nd Row 4 bytes
ip=(a[1] & 0xf0000000)>>28;
cout<<"ip version is "<<dec<<ip<<endl;
This is only part of the code and I haven't included all of it to save space. To give you a better idea, I could email to you separately if you like.
How do I read the second block of data after this? Thanks again for your help!
Last edited by redhead; 03-05-2005 at 06:56 AM.
Reason: Use [ code] tags
|
|
|
03-05-2005, 12:08 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Code:
#include <iostream>
#include <fstream>
#include <ios>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
void print_vector_hex(vector<unsigned>& );
void fetch_block(ifstream&, double& ,vector<unsigned>& );
void fetch_and_print_all_blocks(ifstream& , double&, vector<unsigned>&);
int main(int argc, char *argv[])
{
vector<unsigned> ip_signature;
double arr_time(0);
ifstream ifs("in.txt");
fetch_and_print_all_blocks(ifs, arr_time, ip_signature);
cin.get();
return 0;
}
//fetch_block() assumes a block is:
// a) Starts with a double.
// b) After a double, it continues with unknown numbers of unsinged ints.
//End if newline or EOF is found.
void fetch_block(ifstream& ifs, double& time, vector<unsigned>& vec)
{
string sPeeked;
unsigned val;
istringstream istr;
//Fetch double from block if file is not empty.
if( getline(ifs, sPeeked) )
{
istr.str(sPeeked);
}
istr>>time;
//Now fetch the rest of the block if any unsigned is present.
while( getline(ifs, sPeeked) && sPeeked != "")
{
istr.clear();
istr.str(sPeeked);
istr>>hex>>val;
vec.push_back(val);
}
}
void print_vector_hex(vector<unsigned>& vec)
{
for(unsigned j = 0; j < vec.size(); ++j)
{
cout<<hex<<vec[j]<<endl;
}
}
void fetch_and_print_all_blocks(ifstream& ifs, double& arr_time, vector<unsigned>& ip_signature)
{
while( ifs.peek() != EOF )
{
fetch_block(ifs, arr_time, ip_signature);
//Let's print the arr_time and a the block.
cout<<"arr_time = "<<arr_time<<endl;
print_vector_hex(ip_signature);
cout<<endl;
//Empty the vector for the (possible) new block or data.
ip_signature.erase(ip_signature.begin(), ip_signature.end());
}
}
__________________
|
|
|
03-06-2005, 09:30 AM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
The function "fetch_block()" could be written differently. You'll need to check if this one is more efficient.
Code:
//fetch_block() assumes a block is:
// a) Starts with a double.
// b) After a double, it continues with unknown numbers of unsinged ints.
//End if newline or EOF is found.
void fetch_block(ifstream& ifs, double& time, vector<unsigned>& vec)
{
unsigned val;
char ch = ifs.peek();
//Previous calls of this method leaves a newline behind. Test and remove if so.
if(ch == '\n')
{
ifs.get(ch);
}
//We end up here if newline is removed/non-exist.
//Fetch double from block if file is not empty.
if( ifs>>time )
{
// "istream/ifstream >> variable" leaves a newline behind. Remove it.
ifs.get(ch);
//Now fetch the rest of the block if any unsigned is present.
ch = ifs.peek();
//Stop at next newline (or EOF obviously).
//If there is a next newline, then that must be a second newline: our very
//"block delimiter". We only want to fetch a single block!
while( ch != '\n' && ch != EOF )
{
ifs>>hex>>val;
ifs.get(ch);
vec.push_back(val);
ch=ifs.peek();
}
}
}
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:35 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|