Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File open

Status
Not open for further replies.

simmeone

Programmer
Mar 16, 2000
29
DE
Hi Folks,<br><br>a little question: I open a file for example test.txt and there are a lot of numbers inside. Now I want read this text and replace all &quot;;&quot; thru &quot;,&quot;. At the moment I can open the file, but I don't know how I can replace the signs? Have anybody a idea?<br><br>An example-code would not be bad.<br><br>ThX very much for your help. <img src=
 
Hi SiM,<br><br>One possibility is to copy your test.txt to a new file and<br>replace the characters while doing this.&nbsp;&nbsp;<br>The inner loop will look like this:<br><br><FONT FACE=monospace><br>&nbsp;&nbsp;while((c = fgetc(source)) != EOF)<br>&nbsp;&nbsp;&nbsp;if(';' == c)<br>&nbsp;&nbsp;&nbsp;&nbsp;fputc(',',dest);<br>&nbsp;&nbsp;&nbsp;else <br>&nbsp;&nbsp;&nbsp;&nbsp;fputc(c,dest);<br></font><br><br>
 
I think you need to improve the rpet's code little bit. Since you want to replace the ';' with ','. And when you already read a character the cursor is on the next character. Therefore you need to seek back by one byte and then fput ','. So that it should overwrite the ';'. <br>Thanx<br>Siddhartha <p>Siddhartha Singh<br><a href=mailto:siddhu_singh@hotmail.com>siddhu_singh@hotmail.com</a><br><a href=siddhu.freehosting.net> </a><br>
 
hey buddy,<br>strtok() function is written specifically for such <br>~~~~~~~~ problems. It basically creates tokens depending on the seprator/s U may specify. Just use it and voila no need to make ur code inefficient by reading and writing character by character.<br>And trust me chracter by character operations are a major performance degradation <br><br>Adios<br>amit
 
hi ami123.<br><br>basically you are right about strtok(). but try to<br>think about the specific problem. i'm sure strtok() is<br>_not_ the most efficient way to solve it. just think 2<br>or 3 minutes about how to implement it using this function<br>and the problem will come into focus.<br><br>if you find out an efficient strtok()-solution please post<br>it!! i'm really interested because maybe it's obvious and<br>i just don't see it.<br><br>thank you.<br><br>-- ralf<br><br>
 
hi rpet,<br>&nbsp;&nbsp;&nbsp;this is a piece of code which uses strtok.I have checked it on UNIX systems.the code lloks a bit forty but it can be beutified with a small effort.<br>it has become a bit lenghty coz strtok doesnt handle &quot;\n&quot; too well.<br><br>Functionality: input file is a.a and o/p file is b.b.<br>Non-UNIX users please use ur own #define for LINE_MAX<br>======================================================<br>#include &lt;string.h&gt;<br>#include &lt;sys/limits.h&gt;<br>#include &lt;stdio.h&gt;<br><br>main(void)<br>{<br>char temp[LINE_MAX];<br>char *semi_colon=&quot;;&quot;;<br>char *ptr;<br>FILE *fp,*fp2;<br>fp=fopen(&quot;a.a&quot;,&quot;r&quot;);<br>fp2=fopen(&quot;b.b&quot;,&quot;w&quot;);<br>while(fgets(temp,LINE_MAX,fp)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((ptr = strtok(temp,semi_colon)) == NULL) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(fp2,&quot;%s&quot;,temp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (*ptr != 0x0A) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(fp2,&quot;%s:&quot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;&nbsp;&nbsp;&nbsp;fprintf(fp2,&quot;\n&quot;,ptr);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br>}<br>==========================================================<br>Adios<br>amit<br><A HREF="mailto:crazy_indian@mailcity.com">crazy_indian@mailcity.com</A>
 
I've watched this thread for 5 days now. Ami123 is correct when he refers to the inefficiency of character by character file IO. In general you don't want to do that.<br><br>What you want to do is block IO operations. So you read in a block at a time into a memory buffer. Process the buffer then write the memory buffer into the target file. So, if I understand the original problem correctly to produce a copy of the original file where all semicolons have be replaced with commas it would look like this:<br><br>const int BUFSIZE = 1024;<br>char buf[BUFSIZE];<br>char* psemi;<br>size_t nBytesRead;<br>FILE *inf, *outf;<br>inf = fopen(&quot;data.dat&quot;, &quot;r&quot;);<br>outf = fopen(&quot;fdata.dat&quot;, &quot;w&quot;);<br>memset( buf, 0, BUFSIZE);<br><br>while ( 0 &lt; (nBytesRead = fread((void*)buf, 1, BUFSIZE -1, inf)) ){<br>&nbsp;&nbsp;buf[nBytesRead] = '\0';<br>&nbsp;&nbsp;while ( 0 != (psemi = strchr(buf, ';')))<br>&nbsp;&nbsp;&nbsp;&nbsp;*psemi = ',';<br><br>&nbsp;&nbsp;fwrite( buf, 1, strlen(buf), outf);<br>}<br><br>fclose(inf);<br>fclose(outf);<br><br>If you time the file IO operation and adjust the buffer size you will find the optimal buffer size for a give machine OS combination. 1k, 2k, 4k, 8k, 16k... etc.<br><br>Now all you have to do is supply the file names in place of the hard coded values and add error checking etc.<br><br>Good luck<br>-pete<br>
 
Hi folks,<br><br>thX very much for all answers.<br><br>Greetings SiM
 
hi all,<br><br>somehow i had in mind that the question was how to<br>replace &quot;;&quot; by &quot;,&quot; and _vice_versa_.<br>so i thought that it could become very tricky using<br>strtok(). actually that was not the question. the given<br>example works perfectly.<br>thank you ami123, thank you pete.<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top