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!

Read lines from two text files...

Status
Not open for further replies.

jaredgalen

Programmer
May 28, 2003
34
LT
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//
Code:
for (i=0;i<8;i++)
  sscanf(argv[i],&quot;%x&quot;,&input[i]); //reading from cmd line

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

for (i=0;i<8;i++) 
  data[i]=input[7-i];
                          //putting into the arrays
for (i=0;i<8;i++) 
  cipherData[i]=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
.
.
.
 
There are many ways to do this, the simplest (since you are using sscanf) would probably be to modify your code to read from a stream using fscanf as follows:

Code:
FILE *f1,*f2;
f1 = fopen(&quot;file1&quot;,&quot;r&quot;);  // open file 1
f2 = fopen(&quot;file2&quot;,&quot;r&quot;);  // open file 2

for (i=0;i<8;i++)
  fscanf(f1,&quot;%x&quot;,&input[i]); //reading from file 1

for (i=0;i<8;i++)
  fscanf(f2,&quot;%x&quot;,&cipherInput[i]); // reading from file 2

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

Of course you'll want to replace &quot;file1&quot; and &quot;file2&quot; with your real file names & make sure that f1 and f2 aren't NULL after the fopen statements.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top