newbie question

L

littlebean

Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?
Thanks for help!
James
 
P

Pieter Droogendijk

On 4 Sep 2003 12:18:08 -0700
Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

I don't know what you mean. Change it how? using what algorithm?
 
M

Martin Dickopp

Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?

Sorry, but with only this example, it's hard to guess what the
specifications actually are. Do the `1's and `0's represent characters
which are stored in the file? Or bits? Are the spaces and line breaks also
a part of the file? Are the letters `a' to `e' a part of the file content
or do you just use them to illustrate how to permute the other items? Or
something else entirely?

If the question is how to permute the bits of an integer: As a hardware
engineer, you are probably familiar with the concepts of "bitwise AND",
"bitwise (inclusive) OR", and left and right shift operations. These are
represented in C by the operators `&', `|', `<<', and '>>', respectively.
So if `c' is an `unsigned char' object which contains the original bit
sequence, the expresion

(c & 0x10) >> 4 | (c & 0x8) << 1 | (c & 0x4) >> 1 | (c & 0x2) << 1 | (c & 0x1) << 3

yields the new bit sequence.

Martin
 
E

Eric Sosman

littlebean said:
Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?
Thanks for help!

<off-topic>

awk '{print $2,$5,$4,$3,$1;}' <A >B

</off-topic>

The fact that something *can* be done by writing a C
program doesn't mean it *should* be.
 
J

Julian V. Noble

littlebean said:
Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?
Thanks for help!
James

I think he means he wants to permute the columns in some specific
way.

Probably the simplest way is to create an array of structs, each
containing a character and an integer, then permute the array
elements.

As someone said, it doesn't need C per se--any reasonable language
will let you do the equivalent.

--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
J

Jeff

littlebean said:
Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?
Thanks for help!
James

You didn't describe your question clearly, do you mean something like that ?


#include <stdio.h>
#define THE_LINE_MAX 5+2

int main()
{
FILE *fp, *fp2;
char buffer[THE_LINE_MAX];
char v1, v2, v3, v4, v5;

fp = fopen("FileA.txt", "r");
fp2 = fopen("FileB.txt", "w");

while(fgets(buffer, THE_LINE_MAX, fp))
{
sscanf(buffer, "%c%c%c%c%c", &v1, &v2, &v3, &v4, &v5);
fprintf(fp2, "%c%c%c%c%c\n", v2, v5, v4, v3, v1);
}

fclose(fp);
fclose(fp2);

return 0;
}
 
L

littlebean

sorry, I didn't make my question clear.
a,b,c,d,e represents different signals.
10101,00110 means their value at each clock(each line means one
clock).
I have two machines. One machine only accept format like file A.
It always read a first and then read b and then read c and so on.
Another machine only accept format like file B.
It always read b first and then read e and then read d and so on.
The original file has more than one hundred signals and more than one
thousand lines of vectors. I am wondering if there is any easy way to
covert.

Thanks for everybody's reply!
James
 
L

littlebean

Hi, Jeff,
Thanks for your help. I made my question more clear in my second post.
Your example looks very helpful in my case.
I will try that out.

Thanks agian!
James

Jeff said:
littlebean said:
Hi,
I am a hardware enginneer and know very little about C program.
Recently I need to perform the following function:
Change
file A: a b c d e
1 0 1 0 1
0 0 1 1 0
1 1 1 0 0
TO
file B: b e d c a
0 1 0 1 1
0 0 1 1 0
1 0 0 1 1

Is there any example that can show me how to do this?
Thanks for help!
James

You didn't describe your question clearly, do you mean something like that ?


#include <stdio.h>
#define THE_LINE_MAX 5+2

int main()
{
FILE *fp, *fp2;
char buffer[THE_LINE_MAX];
char v1, v2, v3, v4, v5;

fp = fopen("FileA.txt", "r");
fp2 = fopen("FileB.txt", "w");

while(fgets(buffer, THE_LINE_MAX, fp))
{
sscanf(buffer, "%c%c%c%c%c", &v1, &v2, &v3, &v4, &v5);
fprintf(fp2, "%c%c%c%c%c\n", v2, v5, v4, v3, v1);
}

fclose(fp);
fclose(fp2);

return 0;
}
 
M

Martin Dickopp

sorry, I didn't make my question clear.
a,b,c,d,e represents different signals.
10101,00110 means their value at each clock(each line means one
clock).

It's still unclear to me. Obviously, "signal" doesn't mean here what it
usually means in the C language, but I'm uncertain as to what it means.
Bits? Characters?

Martin


PS: Please don't top-post.
 
L

littlebean

Martin Dickopp said:
It's still unclear to me. Obviously, "signal" doesn't mean here what it
usually means in the C language, but I'm uncertain as to what it means.
Bits? Characters?

Martin


PS: Please don't top-post.

I think that they should be Characters.
 
M

Martin Dickopp

I think that they should be Characters.

In that case, you could just read five of them into five different
variables with the `getc' function. Then use `putc' to write them to
the destination file in the required order.

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,573
Members
47,204
Latest member
MalorieSte

Latest Threads

Top