To print you data in revers for example population = 123456
you want to printf like 654321
. simple way is: read population
in a string instead a int
. and define strrev()
function to print string in reverse
As I understand from your comments your file is a sequence of zipcode
and population
something like:
46804 3450103 37215 1337 47906 46849
and you want to write alternative numbers back to some output file, do like this(read comments to understand code):
#include<stdio.h>
#include<string.h>
#define SIZE 50
void strrev(char* st) {// string reverse function()
char* f = st; // points to first
char* l = st + strlen(st) -1; // points to last char
char temp;
while(f < l){
// swap two chars in string at f & l memory
temp = *f;
*f = *l;
*l = temp;
f++;
l--;
}
}
int main(int argc, char* argv[]){
if(argc!=3) return 0;
FILE* input = fopen(argv[1],"r");
FILE* output = fopen(argv[2],"w");
int zipcode;
char population[SIZE] = {0};
while(fscanf(input,"%d %s\n",&zipcode, population)!= EOF){
// population is always alternative number
strrev(population); // reverse the string
//printf("%s\n",population);
fprintf(output,"%s ",population); // write in output file
}
return 1;
}
And this works as follows:
:~$ cat inputfile
46804 3450103 37215 1337 47906 46849
:~$ ./a.out inputfile outputfile
~$ cat outputfile
3010543 7331 94864
This is one kind of simple solution.
EDIT Because you are commenting you need binary dump file. So I think you need outfile in binary formate: Just open output file in binary mode and use write function. For this I am writing a partial code(again read comments):
FILE* output = fopen(argv[2],"wb");
// ^ open in write binary mode
int val; // an extra variable int
while(fscanf(input,"%d %s\n",&zipcode, population)!= EOF){
strrev(population); // revers string
val = atoi(population); // convert strint to int
fwrite(&val,sizeof(int),1,output); // write in binary file
}