473,657 Members | 2,409 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 1494
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
2818
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 write_file(std::string name, const std::string &s); void find_replace(std::string &s, std::string first, std::string second); bool find_replace_file(std::string name, std::string first, std::string second) {
5
1530
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* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
15
2283
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 the students in a classical (vertically, from top to bottom) way, but make them "sitting" in 50 text boxes, 5 rows 10 columns, how to do it? Thanks in advance.
4
1392
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
5916
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 a database from a menu command while the user is editing a DataGrid. This is unusual in regard to examples and normal practice in that the cell of the DataGrid still has the focus. In all examples I can find, the user normally presses a button on...
4
5476
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
1
2317
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 disapeared. The read is done on a button click. I can read the selected items from the other controls in the repeater, demo page here
5
1544
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 WinXP pc. In tracking it down, it seems that what is being interrupted is either os.remove(), or os.rename(). Has anyone seen this behaviour, or have any clue what is going on? On first pass I would think that both of those calls are single step...
7
5307
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 = infile.read() infile.close() textbuffer.set_text(string)
7
4332
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 folder.. one scheme suggested was to create a string from the names of sorted image files and give it as the cache name.. ie ,if i have one.jpg,three.jpg,new.jpg , i will name the cache as 'newonethree.cache' and everytime i want to check...
0
8395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.