![]() |
|
#1
|
|||
|
|||
(read/write file) newbie need help plzI am pretty new to c programming, any help is appreciated. I have to open a file "fileA" for reading and write something to "fileB" in fileA, it contains text like this: abcdefg "abcdef"; afcdefgdaasaf "abc"; abcdefgfgh""; how do i write in the c code to take all the text iside "" "" and write them into fileB After the process, fileB should look like this abcdef abc Thanks a lot |
|||
|
#2
|
|||
|
|||
|
Hey, welcome to the boards!
File I/O is simple once you get your head around it. For this, you'll have 2 files open for use, reading from FileA, and writing to FileB. So open the files: CPP / C++ / C Code:
Next, we want to read in FileA's contents, one character at a time in a loop. If this character is a double-quote, we will set a variable to be true, so that the next character read in (if it's not a double-quote or an end-of-line) is sent to FileB. CPP / C++ / C Code:
That should do the trick. It's not perfect, I've not closed the files for instance, so fiddle around a little. And make sure you understand how I've done it. GF |
|
#3
|
|||
|
|||
|
Thanks a lot Garth:
I got the idea, but there is just one little thing.. when i compile and run the code you kindly provided, it will display ''textabcdef ''textaabbcc " " " how can i get rid of the " sign in front? Thanks a lot |
|
#4
|
|||
|
|||
|
Hi there:
I was able to get rid of the first " on the previous post now, I've encounter another problem, an extended question: The text inside FileA looks like this: abcdefg "佰擺\百襬" abcedf "aa\bb" and i want to put those inside the double quote to FileB and get rid of the \ sign FileB should look like this: 佰擺百襬 aabb to get rid of the \ between aa\bb is easy.. but not for Chinese Character Since all Chinese character is double-type with "\" on the second byte how do i go about to solve this and make FileB look normal. Thanks a lot |
|
#5
|
|||
|
|||
|
I'm sorry, I'm unfamiliar with how the Chineese character set is represented. What do you mean by a "\" on the second byte? Could you give me some examples to work with?
For double types, there is a class "wchar.h" which supports 'wide' character input. Would this be of any use? See here: www.opengroup.org GF |
|
#6
|
|||
|
|||
read/write FilesDear Garth:
Thanks very much for you help for the past couple weeks, Here is the real problem... I have to write a c program to read data from FileA and convert to FileB FileA and the converted FileB should look like the followings: The blanks in FileA is a blank line (/n) The area i was having the most trouble is when the line contain the '\' sign, I've been trying for quite a long time already, but just couldn't get my output file to look like FileB Once again, thanks a lot for your help, I really realy appreciate it... Sincerely, steve FileA ------------------------------------ 爾耳洱邇餌 二貳 兒而 侶呂屢履旅縷膂褸鋁 侶呂屢履旅縷\膂褸鋁 歐毆甌鷗 偶嘔耦藕 噢 凡墦帆樊煩礬繁藩釩 凡墦\帆樊煩礬繁藩釩 **************************************** ------------------------------------------- FileB ------------------------------------------- unsigned char *ClassTab_B5[] = { (unsigned char *) "", (unsigned char *) "", (unsigned char *) "爾耳洱邇餌", (unsigned char *) "二貳", (unsigned char *) "兒而", (unsigned char *) "", #ifdef WIN32 (unsigned char *) "侶呂屢履旅縷膂褸鋁", #else (unsigned char *) "侶呂屢履旅縷\膂褸鋁", #endif /*WIN32*/ (unsigned char *) "歐毆甌鷗", (unsigned char *) "偶嘔耦藕", (unsigned char *) "噢", #ifdef WIN32 (unsigned char *) "凡墦帆樊煩礬繁藩釩", #else (unsigned char *) "凡墦\帆樊煩礬繁藩釩", #endif /*WIN32*/ (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) "", (unsigned char *) 0 }; --------------------------------------------------- |
|
#7
|
||||
|
||||
|
Hi Steve. I just had a thought when I looked at what you are trying to do. It appears that you are using file b as a c-type file. It sounds like the only problem that you are having is the '\' in the strings. Remember in a C string, the charecter '\' is a special charecter, signifying there is an escape code coming after it. If you want a C string to contain a literal '\', it must be placed as '\\'.
I may have totally misunderstood your problem, but in the samples you have provided, this caught my eye. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading from file | jmenendezr | C++ Forum | 2 | 26-Feb-2006 16:00 |
| Limited file size.... | lament | Apache Web Server Forum | 2 | 31-May-2004 13:35 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| Newbie help, please | Random | C++ Forum | 2 | 02-Dec-2003 12:05 |
| How Do i get php to find out the file type of a file for me? | viperman95833 | MySQL / PHP Forum | 2 | 08-Mar-2003 09:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The