473,748 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems writing stdout to a file

8 New Member
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Oct 2 '07 #1
4 2239
numberwhun
3,509 Recognized Expert Moderator Specialist
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.

open STDOUT,"> ${output}" || die "$0 can't open $ifile";

This is how I'm printing:
print $_;

I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Well, the reason is, you copied from a Windows machine to a Unix based machine. The newline in Windows is a carriage return/line feed, but in Unix it is only a single line feed, no carriage return. Thus, when you view a file on Unix that has been on a Windows system, you will see a ^M at the end of every line. To avoid this, simply do this:

Expand|Select|Wrap|Line Numbers
  1. chomp($_);
  2.  
before the print statement. That will remove the ^M from the end of the line. You should also use chomp on variables after accepting input from the user. The reason? What is the last thing the user does to submit their answer? Hit enter, causing a carriage return/line feed to be entered into the variable.

Regards,

Jeff
Oct 2 '07 #2
mehj123
55 New Member
Hi.. if you just want to remove the ^M characters from the output file, you can use the command
Expand|Select|Wrap|Line Numbers
  1.  dos2unix filename filename
.
this command will remove the ^M s.... actually this command changes the file from the dos format to unix format..

filename can be the same name or different name.. giving same name over writes the existing file..

I dont know if this is any help as this is not related to scripting.. once you get the output file you can use this command to remove ^M..
Oct 3 '07 #3
numberwhun
3,509 Recognized Expert Moderator Specialist
Hi.. if you just want to remove the ^M characters from the output file, you can use the command
Expand|Select|Wrap|Line Numbers
  1.  dos2unix filename filename
.
this command will remove the ^M s.... actually this command changes the file from the dos format to unix format..

filename can be the same name or different name.. giving same name over writes the existing file..

I dont know if this is any help as this is not related to scripting.. once you get the output file you can use this command to remove ^M..
You are correct that dos2unix does achieve this, but the OP would not have to issue an extra system command if they simply used the chomp() command in their code which does the same thing as dos2unix, but on each line before it is written to the file.

Regards,

Jeff
Oct 3 '07 #4
mehj123
55 New Member
Ya Jeff.. you r correct :)
Oct 3 '07 #5

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

Similar topics

7
7592
by: Paul Watson | last post by:
How can I write lines to stdout on a Windows machine without having '\n' expanded to '\r\n'. I need to do this on Python 2.1 and 2.3+. I see the msvcrt.setmode function. Is this my only path? Is it valid to change the mode of stdout? The file.newlines is not writable.
7
3974
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout, name='/tmp/stdout.log', mode='w',bufsize=-1): super(StdoutLog, self).__init__(name,mode,bufsize) self.stdout = stdout
10
1507
by: Nainto | last post by:
Hello, I have posted before about trying to find the status of an FTP uplaod but couldn't get anything to work. After some more searching I found http://groups.google.com/group/comp.lang.python/browse_thread/thread/76be9a994547db4/91917c906cdc04d4?q=ftp+progress&rnum=1#91917c906cdc04d4 but it does not seem to work because it just uploads the file and does not print a . onto the screen. HEre is the code I have when I'm using the code from...
6
3500
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to customer_record SETUP PROGRAM
3
2299
by: kimimaro | last post by:
hi below is my save function that is used to placed data from the C program to a text file for future usage. void save() { FILE *save; int i = 0; save=fopen("employeerecord.txt", "a+");
2
5389
by: goodnamesalltaken | last post by:
Hello fellow python users, I've been working on a basic implementation of a privilege separated web server, and I've goto the point of running a basic cgi script. Basically when the execCGI function in my Unpriv.py program is called a few things should happen, it should fork (which it does), the stdout of the child should be redirected to a given pipe (which it does), and the script should execute using execve(which is has problems...
24
2963
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
12
5092
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i) fwrite(&Data, 1, 1, f); now after running this for long time and pushing millions of bytes, It once misses writing the last byte of fData. Then the further push of bytes is again correct. As i am not using the return value for the
4
2244
by: pcfreak30 | last post by:
ok, i probably have not been registered here long, but i am no noob. I am very experienced in php, but the php cli is a bit of new territory for me. I am trying to create a simple little script that will fetch a webpage and save it to a file. This is just for practice for the records. here is my code: <?php fwrite(STDOUT,"Created By Derrick Hammer\n");
0
8983
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
8822
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9236
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
4592
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
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
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
2774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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.