GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Jan-2004, 01:44
momotx momotx is offline
New Member
 
Join Date: Jan 2004
Posts: 4
momotx is on a distinguished road
Talking

(read/write file) newbie need help plz



I 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  
Old 15-Jan-2004, 09:53
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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:
FILE *input, *output;

input = fopen("FileA", "r");  //opened for reading from
output = fopen("FileB", "w+");  //opened  (created if necessary) to write in

if(input == NULL || output == NULL){ //check the files opened correctly
	printf("Cannot open files, quitting\n");
	exit(1);
}

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:
int write = 0;  //0 not to write, 1 means to write
char temp;

while(!feof(input)){ //while we can read from FileA

	temp = fgetc(input);  //read in character from FileA
	if(temp == '"'){    //that's  ' double-quote ' 
		write = (write+1)%2;  //this flips value of write from 0 to 1, or 1 to 0
	}
	if(temp=='\n' || temp =='\r' ){  //checking for end-of-lines
		write = 0;  //if end of line, stop writing this chunk 
				//protects from one quote missing.
		fputc('\n', output);
	}
	if(write == 1){  //if we're within a couple of double-quotes, print to FileB
		fputc(temp, output);
	}
}

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  
Old 15-Jan-2004, 19:49
momotx momotx is offline
New Member
 
Join Date: Jan 2004
Posts: 4
momotx is on a distinguished road
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  
Old 16-Jan-2004, 03:29
momotx momotx is offline
New Member
 
Join Date: Jan 2004
Posts: 4
momotx is on a distinguished road
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  
Old 16-Jan-2004, 04:51
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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  
Old 27-Jan-2004, 02:25
momotx momotx is offline
New Member
 
Join Date: Jan 2004
Posts: 4
momotx is on a distinguished road

read/write Files


Dear 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  
Old 28-Jan-2004, 13:40
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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 GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 00:18.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.