如何存储fscanf以按相反顺序打印?
嗨,我试图将fscanf存储到一个数据类型中,以便以相反的方式打印它,但无法完成 这是我的密码如何存储fscanf以按相反顺序打印?,c,C,嗨,我试图将fscanf存储到一个数据类型中,以便以相反的方式打印它,但无法完成 这是我的密码 #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE* input; FILE* output; input = fopen(argv[1],"r"); output = fopen(argv[2],"w"); int zipco
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE* input;
FILE* output;
input = fopen(argv[1],"r");
output = fopen(argv[2],"w");
int zipcode, population;
while(fscanf(input,"%d %d\n",&zipcode, &population)!= EOF)
{
fwrite(&population, sizeof(int), 1, output);
}
return 0;
}
我认为,通过在系统上不可移植地输出依赖于机器的、内部表示的
int
,您已经错过了fscanf(提供便携式和独立于机器的数据输入)的要点
你如何从200分中得到2分<代码>200/100结果为2。这与256中的“2”有什么不同吗
您如何从256中获得“5”?您是否使用过模(%
)运算符来获得除法的余数<代码>256%100结果为56<代码>56/10结果为5
那“6”呢?你将如何交换6和2?假设您已经使用上述算法将2提取到“百倍”列中,将6提取到“一倍”列中,您能否交换它们,使6位于“百倍”列中,而两个位于“一倍”列中?您可以使用这种方法以小端、大端或任何您喜欢的混合端进行可移植的输出。。。还有一种相反的方法,可以用小端、大端或任何你喜欢的混合端输入。你能算出来吗
如何从0x1f23中取出0x1f?如何从该号码中提取0x23?有趣的是,您可以使用相同的方法(除法和模法)提取这些“字节”(从技术上讲是八位字节),但基数不同:0x100而不是10,0x10000而不是100,0x1000000而不是1000,等等。事实上,这对于大多数数字系统是正确的:二进制、八进制、十进制、十六进制。。。也许一个更有趣的实验是为LSB(最低有效位优先)和MSB(最高有效位优先)实现可移植的基极负二输入/输出
编辑:事实上,有一种更简单的方法可以实现您在评论中描述的结果:获取整数模10,并将其打印为十进制数字。然后将整数除以10,并继续将该数字作为整数。重复这两个步骤,直到整数为零。要反向打印数据,例如
population=123456
您希望像654321
那样打印f。简单的方法是:读取字符串中的population
,而不是int
。并定义strev()
函数以反向打印字符串
我从您的评论中了解到,您的文件是一系列的zipcode
和population
比如:
46804 3450103 37215 1337 47906 46849
如果您想将可选数字写回某个输出文件,请执行以下操作(阅读注释以理解代码):
这是一种简单的解决方案
编辑因为您正在评论,所以需要二进制转储文件。所以我认为您需要二进制格式的输出文件:只需以二进制模式打开输出文件并使用write函数。为此,我编写了部分代码(再次阅读注释):
要转换endianess,可以参考以下链接: 否则,您可以参考下面的函数作为示例
int convert_endian(int n)
{
int num = 0;
while(n > 0) {
num = (num * 10) + (n %10);
n = n / 10;
}
return num;
}
我觉得您应该使用
fprintf
而不是fwrite
,即使用fprintf(输出,“%d\n”,人口)代码>。另外,在while
循环终止后,作为一种良好的编程习惯,您应该关闭文件
指针。但是这样做不会仍然以相反的顺序打印@ganeshow您将如何将fscanf存储到诸如和int(如果可能)之类的其他内容@Ganesh@user2203801.. 相反的顺序,你的意思是如果人口是123456,你的打印应该是654321吗?我想这个链接可能有用。我明白,但这将如何帮助我反转二进制转储?没有明显的理由编写依赖于整数内部表示的代码(例如,fwrite(&population,sizeof(int),1,output);
。这样做可能会导致未定义的行为(对其他人来说:如果你想对此进行辩论,请在comp.lang.c或##c freenode上这样做,人们知道标准,不介意是否有长时间的辩论)。你想要什么?大端,还是小端?我在你的问题中没有看到这一点。因为指定基是端的先决条件,我们也需要它!一旦指定基(可能是基256,大端意味着第一个0-255“数字”是最大的),不要将二进制(0和1)与机器(0..255)混淆。@Jordan,您可以开始讨论重新排列位置值(例如,将表示1
倍数的位置值替换为表示256*256*256
倍数的位置值),然后您可以根据实际算法指定从大端到小端的转换。我的输入文件类似于46804 3450103 37215 1337 47906 46849(我只需要读取3450103、1337468449)。考虑到我需要将其转换为二进制转储。如果我使用fwrite,这是否可行,因为我必须使用写入输出文件。关于这一点,我按enter bymistake@Jordan阅读最新答案
#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;
}
:~$ cat inputfile
46804 3450103 37215 1337 47906 46849
:~$ ./a.out inputfile outputfile
~$ cat outputfile
3010543 7331 94864
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
}
int convert_endian(int n)
{
int num = 0;
while(n > 0) {
num = (num * 10) + (n %10);
n = n / 10;
}
return num;
}