473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding carriage return within a text file

Claus Mygind
571 512MB
I am creating a text file from an array. I need a specific format with blank lines inserted between lines of data. I need "0D 0A 0D 0A", but all I am getting is "0A 0A".

My app is run on a windows xP platform.

Here is how I store the data in the array
Expand|Select|Wrap|Line Numbers
  1.         array_push($cData,     array(
  2.             $aProject["Project_Name"],
  3.             $aProject["Point_Page1_Depth"],
  4.             $aProject["Water_Unit_Wt"],
  5.             $aProject["Coeff_of_Consol_Factor"],
  6.             $aProject["Name"],
  7.             $aProject["Datum"]));
  8.  
  9. //note the empty cell which in my previous language gave me 0D 0A, but in php only generates 0A
  10.         array_push($cData,     array(""));
  11.         array_push($cData,     array("**POINT"));
  12.  

I steam out the data for download like this
Expand|Select|Wrap|Line Numbers
  1.             /*
  2.             -------------------------------------------------------
  3.             create handle for raw data file
  4.             -------------------------------------------------------
  5.             */
  6.             $rawData = "c:/temp/test.csv";
  7.  
  8.             /*
  9.             -------------------------------------------------------
  10.             look for file if found delete it because "into outfile" 
  11.             will not overwrite existing file
  12.             -------------------------------------------------------
  13.             */
  14.             $fh = fopen($rawData, 'w') or die("can't open file");
  15.             fclose($fh);
  16.             unlink($rawData);
  17.  
  18.             /*
  19.             -------------------------------------------------------
  20.             open file using handle
  21.             -------------------------------------------------------
  22.             */
  23.             $fh = fopen($rawData, 'w') or die("can't open file");
  24.  
  25.             /*
  26.             -------------------------------------------------------
  27.             loop through main array $cData
  28.             and write each sub-array data to file as a line of data
  29.             then close input file.
  30.             -------------------------------------------------------
  31.             */
  32.             foreach ($cData as $fields) {
  33.                 fputcsv($fh, $fields);
  34.             }
  35.  
  36.             fclose($fh);
  37.  
  38.             /*
  39.             -------------------------------------------------------
  40.             create handle for gInt file 
  41.             -------------------------------------------------------
  42.             */
  43.             $outputFileName = 'gINT_upload.txt';
  44.             $fullOutputName = "c:/temp/".$outputFileName;
  45.  
  46.             /*
  47.             -------------------------------------------------------
  48.             look for exiting output file, if found delete it.
  49.             -------------------------------------------------------
  50.             */
  51.             $fh = fopen($fullOutputName, 'w') or die("can't open file");
  52.             fclose($fh);
  53.             unlink($fullOutputName);
  54.  
  55.             /* 
  56.             -------------------------------------------------------
  57.             create final output file 
  58.             -------------------------------------------------------
  59.             */
  60.             $fh = fopen($fullOutputName,"a+") or die("can't open file");
  61.  
  62.             /*
  63.             -------------------------------------------------------
  64.             write into output file
  65.             -------------------------------------------------------
  66.             */
  67.             fputs( $fh,rtrim(file_get_contents($rawData)) );
  68.  
  69.             /* 
  70.             -------------------------------------------------------
  71.             close output file 
  72.             -------------------------------------------------------
  73.             */
  74.             fclose($fh);
  75.  
  76.             /*
  77.             -------------------------------------------------------
  78.             force download request to open on client.
  79.             -------------------------------------------------------
  80.             */
  81.             if($result) {
  82.                 header('Pragma: public');
  83.                 header('Expires: 0');
  84.                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  85.                 header('Cache-Control: public');
  86.                 header('Content-Description: File Transfer');
  87.                 header('Content-Type: plain/text');
  88.                 header('Content-Disposition: attachment; filename='.$outputFileName);
  89.                 header('Content-Transfer-Encoding: ascii');
  90.                 header('Content-Length: ' . filesize($fullOutputName));
  91.  
  92.                 @readfile($fullOutputName);
  93.  
Aug 6 '12 #1

✓ answered by Claus Mygind

Well fputs( $fh, "\r\n" ); is the way I got it to work.

2 5975
Claus Mygind
571 512MB
I see my problem is "fputcsv($fh, $fields);" - this only adds 0a to the end of each line

whereas I can add 0a 0d with "fputs ($fh, "\r\n" );"

So the question becomes is there a way to add the "0d" with fputcsv?

The nice feature with fputcsv it it automatically adds delimiters around my text that contains commas.
Aug 6 '12 #2
Claus Mygind
571 512MB
Well fputs( $fh, "\r\n" ); is the way I got it to work.
Aug 6 '12 #3

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

Similar topics

4
by: Rob Lemieux | last post by:
Using Visual Basic 6.0 I want the program to be able to print the carriage return. txtDisplay.Text = "Welcome to" + Chr(13) + "my demo." All I get is Welcome to|my demo.
3
by: gogomei | last post by:
I have a text file like following and need to read out the names of each person. Since the only accurate info is there is a null line before starting a new name, I have written following code to...
3
by: Simon Middlemiss | last post by:
I have a multiline textbox which I would like populated in the following manner. "Line1 Line2 Line3 Line4" etc. I know how to do it in code, but is there a way to do it in the design view?
1
by: Jason | last post by:
hi, all, after I read a string from a text file, which contains a hidden character (carriage return) some where in the string, how can I remove this hidden character, because when I populated it...
11
by: Michael McGarry | last post by:
Hi, I am reading strings from a text file using fscanf("%s", currToken); This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the...
4
by: John | last post by:
I can create text file, but how can I create a text file where the values are at the same location on every line? I want to define the location where the values starts and define the length of...
5
by: majidmajid | last post by:
hay...i want to know how i can sort the records within text file...using c
1
by: nur123 | last post by:
Thanks in advance who will look at it. I have been encountering an issue which I can’t find a way out of it. What my pgm does: It (java codes) reads oracle table data and creates flat text...
11
by: evenlater | last post by:
My db allows the user to send email via CDO. The body of the email is determined in code. I have built an email form with To, CC and Subject lines and a large text box for the body of the message...
0
by: cyuno | last post by:
I am having problems in IE with the following text box javascript code. In Firefox the line breaks and carriage returns work perfectly but not in IE: Here is the javascript: function repp(){...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.