File output characters, ask

Category: C/C++ -> C Author: luoguowei Date: 2003-06-15 21:00:52
 
luoguowei
2003-06-15 21:00:52
Because of the need to extract from a txt file area code and phone area code corresponding to each of the city, no province name, all I have established a three structure types, long char char, because the original text in the provincial public and City name with a space, when you can all read automatically distinguish the original documents required content 0.txt phone area code and city names written in a fixed array format A.txt go, finally out of the area code is correct , but the character is garbled, I know that is a double-byte characters, but how could he realized characters read and output it? This is my 0.txt

contents inside
1300000 | Beijing Beijing | China Unicom 130 card
1300001 | Changzhou | Unicom wishful Card
1300002 | Unknown | Unknown
1300003 | Unknown | Unknown
1300004 | Unknown | Unknown
1300005 | Unknown | Unknown
1300006 | Nanjing | Unicom 130 card
1300007 | Unknown | Unknown
1300008 | Unknown | Unknown
1300009 | Unknown | Unknown
1300010 | Beijing Beijing | China Unicom 130 card
1300011 | Beijing Beijing | China Unicom 130 Card
1300012 | Tianjin | Unicom 130 card
1300013 | Tianjin | Unicom 130 card
1300014 | Tianjin | Unicom 130 card
1300015 | Shandong Zibo | Unicom 130 card
1300016 | Yantai | Unicom 130 card
1300017 | Jinan | Unicom 130 card
1300018 | Tianjin | Unicom 130 card
1300019 | Tianjin | Unicom 130 card
1300020 | Shanghai Shanghai | Unicom 130 card
1300021 | Shanghai Shanghai | Unicom 130 card
1300022 | Shanghai Shanghai | China Unicom 130 card
1300023 | Shanghai Shanghai | Unicom 130 card
1300024 | Shanghai Shanghai | Unicom 130 card
1300025 | Nanjing | Unicom 130 card
1300026 | Nanjing | Unicom 130 card
1300027 | Yantai | Unicom 130 card
This is A.txt results obtained
mobile_number [] = {{0,} ,
{1300001, Ning draft Dan |??},
{1300002, chain � Jin},
{1300003, chain � Jin},
{1300004, chain � Jin },
{1300005, chain � Jin},
{1300006, Hua, Beijing |??},
{1300007, chain � Jin},
{1300008, chain � Jin},
{1300009, chain � Jin},
{1300010, Acta, Beijing |??},
{1300011, Acta, Beijing |??},
{ 1300012, Chan ╂ touch |??},
{1300013, Chan ╂ touch |??},
{1300014, Chan ╂ touch |??},
{1300015, Di Fanpi |??},
{1300016, Hot-Zengzhuo |??},
{1300017, Xian Yi Han |??},
{1300018, Chan ╂ touch |??},
{1300019, Chan ╂ touch |??},
{1300020, Juan Cong pound |??},
{1300021, Juan Cong pound |??},
{ 1300022, Juan Cong pound |??},
{1300023, Juan Cong pound |??},
{1300024, Juan Cong pound |??},
{1300025, Hua, Beijing |??},
{1300026, Hua, Beijing |??},
there is a problem, that is why the first line is 0 then?
Thank enlighten heroes! !
fanjie879
2003-06-15 21:36:53
just forgot to code
# include "stdio.h"
# include "string.h"
# include "ctype. h "
# include" stdlib.h "
# include" process.h "
typedef struct
{
long number;
char province [8];
char city [8];
} num_city_struct;

num_city_struct num_city [1000];

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 Writetxt (char * p, num_city_struct *)
{
FILE * fp;
unsigned i = 0, j , k;
char temp [60];
char m [8];
if ((fp = fopen (p, "w")) == NULL ) / / Î Ä ¼ þ ´ ò ¿ ª & Ecirc ; § ° Ü
{
fprintf (stderr, "Can't open% s for writing. \ n", p);
exit ( 1);
}
fprintf (fp, "mobile_number [] = {");
for (i = 0; i <1000; i + +)
{
memset (temp, 0,60);

fprintf (fp, "{");

/ / fprintf (fp , "% Ld,% s", num_city [i]. number, num_city [i]. city); / / ½ « ½ á & ; sup1; ¹ Ð ´ È ë Ð Â Î Ä ¼ þ
fprintf (fp, "% Ld", num_city [i]. number);
fprintf (fp, ",");
fprintf (fp, "% s", num_city [i]. city);
fprintf (fp, "},");

fprintf (fp, "\ n");
}
fprintf (fp, "}");
fclose (fp);

}
void main ()
{
char * pr = "0.txt";
char * pw = "A.txt";
Readtxt (pr, num_city);
Writetxt (pw, num_city);

}
this is my code, character input and output is not a specified format it? ?
evihack
2003-06-15 21:53:43
MSDN: fwprintf

Print formatted data to a stream.

Function Required Header
fprintf <stdio.h>
fwprintf <stdio.h>

int fprintf (FILE * stream, const char * format [, argument] ...);
int fwprintf (FILE * stream, const wchar_t * format [, argument] ...);
sd_wk
2003-06-15 22:08:57
dev-cpp try on
qzgfzk175m
2003-06-15 22:35:00
The problem does not belong to the Chinese character input problem, but the file format of each line inconsistencies caused by (some lines and some spaces line is not), so use scanf does not read correctly;
even without spaces, grabbed the program can not be read correctly.
one is not given a separator, such as:
sscanf (temp, "% ld |% s |% s" ;, ....
two string in sscanf Do not use the address-character &.
some other errors, I do not eleven enumerated.
recommended to write a function to handle read, I grabbed a little bit of code changed, it should be read out (not tried, the first statement, it is not the best code):

# include "stdio.h"
# include "string.h"
# include "ctype.h"
# include "stdlib.h" ;
# include "process.h"
typedef struct
{
char number [8];
/ / char province [8];
char city [8];
} num_city_struct;

num_city_struct num_city [1000];

int GetText (char * source, char * s, int num)
{
int i = 0;
while (source [num]! = 0x0d ; && source [num]! = '' && source [num]! = '|')
s [i ; + +] = source [num + +];
s [i] = 0;
return num;
}

int Readtxt (char * p)
{
FILE * fp;
unsigned i = 0;
char temp [60];
int num;
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);
num = GetText (temp, num_city [i] . number, 0);
num = GetText (temp, num_city [i]. city, num + 1);
if (temp [num] == '')
num = GetText (temp, num_city [i]. city, num + 1);
/ / sscanf (temp, "% Ld |% s% s |", & num_city [i]. number, num_city [i]. province, num_city [i]. city);
i + +;
}
fclose (fp);
return i;
}
void Writetxt (char * p, int count)
{
FILE * fp;
unsigned i = 0, j, k;
char temp [60];
char m [8];
if ((fp = fopen (p, "w")) == NULL) / / Î Ä ¼ & ; thorn; ´ ò ¿ ª Ê § ° Ü
{
fprintf (stderr, "Can't open % s for writing. \ n ", p);
exit (1);
}
fprintf (fp," mobile_number [] = {\ n ") ;
for (i = 0; i <count; i + +)
{
memset (temp, 0,60);

fprintf (fp, ; "{");

/ / fprintf (fp, "% Ld,% s", num_city [i]. number, num_city [i]. city); / / ½ « ½ á ¹ ¹ Ð ´ È ë Ð & Acirc; Î Ä ¼ þ
fprintf (fp, "% s", num_city [i]. number);
fprintf (fp, " ;, ");
fprintf (fp,"% s ", num_city [i]. city);
fprintf (fp,"}, ");

fprintf (fp, "\ n");
}
fprintf (fp, "};");
fclose (fp);

}
void main ()
{
char * pr = "d: \ \ 0.txt";
char * pw ; = "d: \ \ A.txt";
int count = Readtxt (pr);
Writetxt (pw, count);

}
tukibana
2003-06-15 23:05:23
added:
code issues, we found two spaces gone (html sake), please note.
1,
while (source [num]! = 0x0d && source [num]! = 0x20 && source [num]! = '|' )
2,
if (temp [num] == 0x20)
hyr1227
2003-06-15 23:33:57
with wcin and wcout and wchar_t
xiaoseyefeng888
2003-06-16 00:13:37
Thank you upstairs Members of the answer, the problem has been resolved, specifically the Wrong not too clear, long with the following character fscanf can automatically distinguish between, I would do so, and, I aim is to take the city name, if it is unknown if no spaces , then the city [8] to take is blank if there is space, then I can get the character, and then deal with what rid | back on ok! maozefa brother of the code is very good, if then I process the text is not so Format standard, then, can not do this without you ~