how to remove space between characters of a string using wide char to multibytes?

这一生的挚爱 提交于 2020-01-14 05:13:11

问题


I have a file open in winhex look like follow.

1F 00 48 3A 18 00 00 00 53 00 70 00 6F 00 75 00 73 00 65 00 5F 00 61 00 7A 00 61 00 6D 00 00 00

I am reading the above hex data from file and write it to a text file . My code is as follow.

#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
int main()
{
FILE *pFile, *tempFile;
char *Main_buffer;
int nOfRecord, TotalSize, data=0;
pFile = fopen("C:\\wab files\\Main.wab", "rb"); 
if(pFile == NULL)
{
    fputs("file error", stderr);
    exit(1);
}
tempFile = fopen("C:\\myfile.text","wb");
if(tempFile == NULL)
{
    fputs("file not open", stderr);
    exit(2);
}
 fread(&nOfRecord, 1, 4, pFile);
 fread(&TotalSize, 1, 4, pFile);
 data = TotalSize;
 char* Main_buffer = (char*)malloc(data*sizeof(data));
 fread(Main_buffer, 1, TotalSize, pFile);
 fwrite(Main_buffer, 1, TotalSize, tempFile);
 free(Main_buffer);
 return 0;
}

This code gives a text file in which data is written as follow.

p a l @ g m a i l . c o

In above data there is a space between each character . How to remove space from the data, and write in a text file . Please explain by writing some code as an example for wide char to multi bytes . Thanks you in advance .


回答1:


There are basically 2 ways of doing it:

  1. Manually removing spaces from the generated ASCII thing.

  2. Use a library to do the work for you (of course if you are allowed to). My preference goes toward http://en.wikipedia.org/wiki/Iconv

which (as said on the Wikipedia page) has a free implementation in GCC so you can try to play with it. And here the link to the Linux lib: http://www.gnu.org/software/libiconv/#TOCintroduction

UPDATE

Here is an example in C of how to use libiconv: http://www.gnu.org/software/libc/manual/html_node/iconv-Examples.html




回答2:


Try using strtok() from < string.h > .



来源:https://stackoverflow.com/questions/19705070/how-to-remove-space-between-characters-of-a-string-using-wide-char-to-multibytes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!