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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from two text files....

Status
Not open for further replies.

jaredgalen

Programmer
May 28, 2003
34
LT
There seems to be a few more people using this forum so here's the Question.
I have a program that reads in a 32-digit hex number from the command line:
ie. findkey a3 1f 2e 3c 4e 5a f6 a3 1f 2e 37 d5 66 1a 33 3c

I take this and split it up into two arrays.
The first 8 go into the input[] array.
The next 8 go into the cipherInput[] array as in the code below.
What I need to do is instead of taking it from the command line, I want to read it from two files.
The first 8 will come from one file, the last 8 will come from another file.
I had been passing it from java, but that was a temp hack.
Can someone help me with this.
//current code//

for (i=0;i<8;i++)
sscanf(argv,&quot;%x&quot;,&input); //reading from cmd line

for (i=8;i<16;i++)
sscanf(argv,&quot;%x&quot;,&cipherInput[i-8]);

for (i=0;i<8;i++)
data=input[7-i];
//putting into the arrays
for (i=0;i<8;i++)
cipherData=cipherInput[7-i];

In summary I need to:
Read in from the first file and put that into input[]
Read in from the second file and put that into cipherInput[]
I have to read from the same line number in each file, both files are gauranteed to have the same number of lines.

Each file format is:
a3 1f 2e 3c 4e 5a f6 a3
1f 2e 37 d5 66 1a 33 3c
.
.
.

 
Here's what I'm trying, reading from the file, one line,
putting that into a char array.
I tried using sscanf with that array but it didn't work.
I not familiar with this at all so could really use some help.
I know the indexing in the sscnaf line is all wrong I'm just trying to get it to work.
Code:
char buffer[24];
ifstream plainFile (&quot;output/example.txt&quot;);
if (! plainFile.is_open())
  {cout << &quot;Error opening file&quot;; exit (1);}

  while (! plainFile.eof()) {
    plainFile.getline (buffer,24);
    for (i=0;i<8;i++){
      sscanf(buffer[i],&quot;%x&quot;,&cipherInput[i]);
      printf(&quot;%x&quot;,cipherInput[i]);
    }
  }
 
The program you're writing there is in C++, not C. However....

You've at least diagnosed the problem correctly.
Code:
buffer[i]
in the sscanf line references a single char - the one within the i-indexed char array. Since you're using C++ anyway, this might be simpler:

Code:
string buf;
ifstream plainFile (&quot;output/example.txt&quot;);

if (!plainFile.good()) {
   cout << &quot;Error opening file&quot;; exit (1);
}

for (i = 0;i < 8 && !plainFile.eof();i++) {
   plainFile >> buf;
   sscanf(buf, &quot;%x&quot;, &cipherInput[i]);
   printf(&quot;%x\n&quot;, cipherInput[i]);
}

-
 
Thanks for the reply, heres what I ended up using.
Sorry for posting in the C forum but the C++ one seemed to have no activity.
I learnt a lot taday, thanks.
Code:
plainFile.getline (plainBuffer,24); 
*plainPoint = &plainBuffer[0];      

for (i=0;i<8;i++){
sscanf(*plainPoint,&quot;%x&quot;,&input[i]);
*plainPoint = *plainPoint+3;
}

jG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top