File output characters, why is garbled

Category: C/C++ -> C Author: Crazy_magic Date: 2012-08-20 20:02:21
 
Crazy_magic
2012-08-20 20:02:21
I define an array of pointers
const char * city_name [] = {
"Beijing",
"Shanghai",
"Tianjin" ,
"Chongqing",
"Handan",
"Shijiazhuang",
"Baoding",
"Zhangjiakou" ,
"Chengde",
"Tangshan",
"Langfang",
"Changzhou",
"Hengshui" ,
"Xingtai",
"Qinhuangdao",
"Quzhou",
"Hangzhou",
"Huzhou" ,
"Jiaxing",
"Ningbo",
"Shaoxing",
"Taizhou",
"Wenzhou" ,
"Yeosu"
}
then written into a text program which requires the inside, I use fprintf (fp, "% s", city_name [5]); out of all is garbled, and, I now have the text read out from the character is Chinese characters, is read out through the char type, how characters defined here compare with it? strcmp can compare the results? There I read out from the text using fgets storage array to the char type, by fprintf print out the displayed characters are normal, so do not know why in the function definition inside their own output Kanji why there is a problem? ? ? Please expert advice?
woshihuster
2012-08-20 20:11:41
cout << cout.setw () << city_name [5] << endl; look at this how!
liuluchuan106
2012-08-20 20:25:22
cout << setw (12) << city_name [5] << endl; look at this how!
a120220a
2012-08-20 20:33:17
Will UPCC
cout << setw (12) << city_name [5] << endl
setw (12) is doing the ? In that header file defines?
chinaunicom0735
2012-08-20 20:42:56
Manipulators defined in <iomanip>

setw (int w) Sets the field width to w
w7110378
2012-08-20 20:45:10
this is only output to standard output, I now find, from the text read kanji fprintf fgets then wrote the text no problem from the program read kanji (Chinese characters is above their own definition) in the output to the standard output stream cout is not a problem, if these two methods intersect, then, is garbled! Read from the text to the standard output stream and read from the program's output to the text is garbled, I now want to compare these two codes do? Trouble, it seems that only the characters in the program Esen into txt file, and then read out, so may not be a problem, huh, huh! But not willing to, ah, do not understand why? ?
Rechin001
2012-08-20 20:50:21
compare these two kanji characters to draw the same
yxiong33
2012-08-20 20:52:35
# include <stdio.h>
# include <cstdlib>

const ; int N = 24;
const char * city_name [] = {
"Beijing",
"Shanghai",
" ; Tianjin ",
" Chongqing ",
" Handan ",
" Shijiazhuang ",
" Baoding ",
" ; Zhangjiakou ",
" Chengde ",
" Tangshan ",
" Langfang ",
" Changzhou ",
" ; Hengshui ",
" Xingtai ",
" Qinhuangdao ",
" Quzhou ",
" Hangzhou ",
" ; Huzhou ",
" Jiaxing ",
" Ningbo ",
" Shaoxing ",
" Taizhou ",
" ; Wenzhou ",
" Yeosu "
};

int main ()
{

FILE * fp;
fp = fopen ("out.txt", "w");
; if (fp == NULL)
{
printf ("File was not opened. \ n");
exit (1);
}
int i;
for (i = 0; i <N; + + i )
{
/ / fputs (city_name [i], fp);
fprintf (fp, "% 10s", city_name [i]);
}
; system ("pause");
return 0;
}

my dev-c + + under debugging through, no problem ah
fputs also possible

xingyu1027
2012-08-20 21:01:33
For general use the unicode character encoding, it is recommended to see xml no nonsense Chapter

with a wide character comparisons
wchar like,
bestlucky
2012-08-20 21:30:29
Compare characters:

wcscmp <string.h> or <wchar . h>

int wcscmp (const wchar_t * string1, const wchar_t * string2);
abclodge
2012-08-20 22:06:01
posted my code, please help experts to see what the specific problem running
# include "stdio.h"
# include "iostream.h"
# include "string. h "
# include" ctype.h "
# include" stdlib.h "
# include" process.h "
# include" TCHAR.h "
# include" iomanip.h "
# define NULL 0
# define MAX_CITY 315

typedef ; struct
{
long number;
char province [20];
char city [30];
} num_city_struct;

typedef struct
{
char city [30];
} name_city_struct;
name_city_struct city_name [MAX_CITY];

num_city_struct ; num_city [1000];
void Clear ()
{
int i, j;
for (i = 0; i <= 1000; i + +)
for (j = 0; j <= 30; j + +)
num_city [i]. city [j] = 0;
}
void Readtxt (char * p , num_city_struct *)
{
FILE * fp;
unsigned i = 0;
char temp [60];
if ((fp = fopen (p, "r")) == NULL)
{
fprintf (stderr, "cann't open% s for reading. \ n", p);
exit (1);
}
while (! feof (fp))
{
memset (temp, 0, 60 );
fgets (temp, 60, fp);

sscanf (temp, "% Ld% s% s", & num_city [i]. number, & num_city [i]. province, & num_city [i]. city);
i + +;
}
fclose (fp);
}

void Readtxt1 (char * p, name_city_struct *)
{
FILE * fp;
unsigned i = 0;
char temp [60];
if ((fp = fopen (p, "r")) == NULL)
{
fprintf (stderr, "cann't ; open% s for reading. \ n ", p);
exit (1);
}
while (! feof (fp))
{
memset (temp, 0, 60);
fgets (temp, 60, fp);

sscanf (temp, "% s ", & city_name [i]. city);
i + +;
}
fclose (fp);
}
void Writetxt (char * p ;, num_city_struct *)
{
FILE * fp;
int i = 0, j, k, f, count = 0;
char m [30];
int code = 0;
int flag = 0;
if ((fp = fopen (p, "w")) == NULL) / /??? t'ò? aê § ° ü
{
fprintf (stderr, "Can't open% s for writing. \ n ", p);
exit (1);
}

fprintf (fp," const mobile_number_struct m_1300 [ ] = {");
fprintf (fp," \ n ");
for (i = 0; i <1000; i + +)
{
code = 0;
memset (m, 0,30);
count + +;
for (j = 0; j <30; j + +)
{
if (num_city [i]. city [j] == '|')
{

strncpy (m, num_city [i]. city, j);

}
}
flag = 0;
for (f = 0; f <30; f + +)
{
if (m [f]! = 0)
flag = 1;
}
if (flag == 0)
{
fprintf (fp, " ; 10000 ");
}
else
{
for (k = 0; k <MAX_CITY; k + +)
{
if (strcmp (m , city_name [k]. city) == 0)
{
code = k;
fprintf (fp, "% x", code);
break;
}
}
if (code == 0)
fprintf (fp, "60000");

}

fprintf (fp, "\ n");

}

fclose (fp);

}
void main ()
{

char * pr9 = "9.dat";
char * pw9 = " A9.dat ";
char * pp =" m.dat ";

Readtxt (pr9, num_city);
Readtxt1 (pp, city_name) ;
Writetxt (pw9, num_city);
Clear ();

}
Mysheepbaby
2012-08-20 22:31:20
9.dat reads as follows, originally 1000 lines , due to the spatial relationships on the stickers so much meaning to understand on the line
1369000 | Guangdong Chaozhou | Mobile GSM card
1369001 | Guangdong Chaozhou | Mobile shengzhouxing card
1369002 | Guangdong ; Chaozhou | Mobile GSM card
1369003 | Guangdong Chaozhou | Mobile GSM card
1369004 | Guangdong Chaozhou | Mobile GSM card
1369005 | Guangdong Chaozhou | Mobile shengzhouxing card
1369006 | Guangdong Chaozhou | Mobile shengzhouxing card
1369007 | Guangdong Chaozhou | Mobile shengzhouxing card
1369008 | Guangdong Chaozhou | Mobile GSM card
1369009 | Guangdong Chaozhou | Mobile GSM card
1369010 | Foshan | Mobile shengzhouxing card
1369011 | Foshan | Mobile shengzhouxing card
1369012 | Foshan | Mobile shengzhouxing card
1369013 | Foshan | Mobile shengzhouxing card
1369014 | Foshan | Mobile shengzhouxing card
1369015 | Foshan | Mobile shengzhouxing card
1369016 | Foshan | Mobile shengzhouxing card
1369017 | Foshan | Mobile shengzhouxing card
1369018 | Foshan | Mobile shengzhouxing card
1369019 | Foshan | Mobile shengzhouxing card
1369020 | Guangdong Foshan | Mobile prepaid card
1369021 | Foshan | Mobile prepaid card
1369022 | Foshan | Mobile prepaid card
1369023 | Foshan | Mobile prepaid card
1369024 | Foshan | Mobile prepaid card
1369025 | Foshan | Mobile prepaid card
1369026 | Foshan | Mobile prepaid card
1369027 | Foshan | Mobile prepaid card
1369028 | Foshan | Mobile prepaid card
1369029 | Foshan | Mobile prepaid card
fkegj
2012-08-20 22:49:57
m.dat reads as follows: It is also a lot of places in short, meaning place on the line

Beijing
Shanghai, Tianjin, Chongqing

Handan

Shijiazhuang, Baoding, Chengde

Tangshan, Zhangjiakou

Langfang, Cangzhou, Hengshui

Xingtai
Qinhuangdao
Quzhou

Hangzhou, Jiaxing, Huzhou

Taizhou, Shaoxing, Ningbo

Lishui, Jinhua Wenzhou

Zhoushan

Tieling
Shenyang, Dalian, Anshan

Fushun, Benxi, Dandong

Jinzhou, Yingkou
Fuxin

Chaoyang Liaoyang, Panjin
Huludao

Xiangcheng
Wuhan Ezhou
Xiaogan
Huangzhou
Yellowstone Xianning
Jinsha

Enshi
Yichang, Shiyan
Jingmen
with jujube
Jianghan

Nanjing, Suzhou, Wuxi

Zhenjiang, Yangzhou, Nantong

Xuzhou, Yancheng

Huai'an Huaiyin

Lianyungang
Changzhou
Taizhou
Hailar
Hohhot
Baotou
Wuhai
Jining
Chifeng, Tongliao

Dongsheng
Kanawha
Xilinhaote
Ulan
Alashan

Nanchang, Jiujiang, Xinyu

Shangrao
Linchuan
Yichun
Gian