473,503 Members | 4,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

temp=tmpfile() lose all its contents?

2 New Member
Dear there,

I wrote a simple all->unix ascii file converter on fly, see the comments below. I am using temp=tmpfile() to hold contents for later processing, but it seems lost later on.

Expand|Select|Wrap|Line Numbers
  1. /* FILE* file is assigned before calling this routing. 
  2. After calling this routine, 'file; is supposed to point to the temporary file stream 
  3. with processed content. However, I found later that file has no content at all.
  4. */
  5.  
  6. /** 
  7.  * simple text convert: convert all to Unix line ending
  8.  * I use fgets() to read a line, which stops whenever \n (LF) is met. However, different line ending could be used:
  9.  * DOS/Windows: CR LF
  10.  * Unix: LF
  11.  * MAC OS X: LF
  12.  * MAC (prior to MAC OS X): CR
  13.  * this convert is simple since it is only for use with fgets(), strtok(), and strtof()
  14.  * strtok() will skip leading 'sep' when processing tokens, in this file sep=' \t,'; strtof() will
  15.  * skip leading white space as specified by isspace(). Hence I replace CR LF with LF SPACE; and CR with LF.
  16.  */
  17. void trimcarriage(){
  18.   FILE* temp=tmpfile();//see p483 k.n.king
  19.   int ch, chnext;
  20.   while(ch=getc(file) != EOF){
  21.     if (ch != '\r'){//straight copy
  22.       putc(ch, temp);
  23.     }else{
  24.       putc('\n',temp);//replace it with \n
  25.       if (chnext=getc(file) != '\n'){
  26.     ungetc(chnext, file);//put it (including EOF) back
  27.       }//otherwise '\n' is not wrote into temp
  28.     }
  29.   }
  30.   fclose(file);
  31.   rewind(temp);
  32.   file=temp;
  33. }
  34.  
May 9 '12 #1
2 1483
weaknessforcats
9,208 Recognized Expert Moderator Expert
Are temp and file both FILE* to the same file?

If so, you have a problem because you cannot have two active FILE* to the same file.
May 9 '12 #2
learner007
2 New Member
I found the mistake in the statement:
ch=getc(file) != EOF
which should be
(ch=getc(file)) != EOF
due to operator precedence.
May 9 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
2789
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
5
1523
by: Christopher Benson-Manica | last post by:
Is there a way to get the entire contents of the current document as a string? I want to send the document contents to a markup validation service. -- Christopher Benson-Manica | I *should*...
15
2272
by: Paul T. Rong | last post by:
Hi everybody, This time a very very difficult case: I have a table "tblStudent", there are 50 students, I also made a form "frmStudent" based on "tblStudent", now if I don't want to show all...
4
1389
by: Pilocarpine | last post by:
Hi, It's been a while since I posted but good ol' google groups is confusing me these days and not giving me nice answers. I have this field in a table with some data HI1 HI2 HI1
11
5901
by: Junkguy | last post by:
I need some help programmatically causing a row in a DataGrid to "flush" its contents to its bound data (in Visual Studio 6 using Windows Forms with C#). My issue is I want to send an update to...
4
5449
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
1
2301
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has...
5
1538
by: Russell Warren | last post by:
I've got a case where I'm seeing text files that are either all null characters, or are trailed with nulls due to interrupted file access resulting from an electrical power interruption on the...
7
5296
by: google | last post by:
Hi, I'm using Python with pygtk and have this problem - I have read the contents of a file into the text buffer with this code, infile = open("mytextfile", "r") if infile: string =...
7
4325
by: devnew | last post by:
hi i am trying to create a cache of digitized values of around 100 image files in a folder..In my program i would like to know from time to time if a new image has been added or removed from the...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.