473,668 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading/writing a file?

I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î

the columns are defined by the *#, so that line would break apart
like:
A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234Î
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished? I'm familiar with
requesting and manipulating data from mysql but this reading/writing
to a txt file is completely new to me. I have about 1 years worth of
off and on PHP experience. Any help is greatly appreciated. Thank you

Jason
Jul 17 '05 #1
2 2871

"Dot Kom" <jr*****@hardyn et.com> wrote in message
news:5c******** *************** ***@posting.goo gle.com...
I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î

the columns are defined by the *#, so that line would break apart
like:
A*1King Richard Dale *3Welton Orchard Rd *4Petersburg *5257-1234Î
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished? I'm familiar with
requesting and manipulating data from mysql but this reading/writing
to a txt file is completely new to me. I have about 1 years worth of
off and on PHP experience. Any help is greatly appreciated. Thank you

Jason


This can be solved in a few steps, but you'll need to read up on the
associated functions yourself.

Step 1: If the file is not too large, you can load it into an array variable
with the "file" function:

$array_data = file("/www/htdocs/data/thefile.dat");

Step 2: You can slice up each line of the file at the delimeters with the
"explode" function:

foreach ($array_data as $array_line) {
$array_elements = explode("*",$ar ray_line);
// $array_elements[0] would have "A"
// $array_elements[1] would have "1King Richard Dale", etc.
// Process items here; remember to extract the leading number character(s)
}
Jul 17 '05 #2
jr*****@hardyne t.com (Dot Kom) wrote in message
news:<5c******* *************** ****@posting.go ogle.com>...

I've got this text file full of lines like:

A*1King Richard Dale*3Welton Orchard Rd*4Petersburg* 5257-1234Î
Wow... A weird setup, isn't it?
Basically, what I need to do is read this file and I need to write
field 1, 4 and 5 which need to go into a text file in tab delimited
format like:

King Richard Dale Petersburg 257-1234

What can I do to get this task accomplished?


Something like this:

$in = fopen ('your_old_file .txt', 'r');
$out = fopen ('your_new_file .txt', 'w');
if (!$out) {
die ('Cannot open the output file for writing. ');
}
if (!$in){
die ('Cannot open the input file for reading. ');
}
while (! feof ($in)) {
$items = explode ('*', fgets ($in, 4096));
$line = '';
foreach ($items as $item) {
$num = $items{0};
if (($num == 1) or ($num == 4)) {
$line .= substr ($item, 1) . "\t";
}
if ($num == 5) {
$line .= substr ($item, 1) . "\r\n";
}
}
if ($line <> '') {
fputs ($out, $line);
}
}
fclose ($in);
fclose ($out);

Cheers,
NC
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9824
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile = tmpfile(); /* write into tmpFile */ ...
2
3064
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an array). I cannot change anything in the program but can add some features by which I can convert this array of data into a file. The easiest thing would be to write the data into a file (in hard disk) and use it. But I will be working on thousands...
3
9506
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
2
2007
by: John Salerno | last post by:
I wrote this code just to experiment with writing to and reading from a file. It seems to work fine when writing, but when reading the file, it only prints the filepath to the screen, not the file contents. System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\positions.txt"); System.IO.StringReader myFile = new System.IO.StringReader(@"C:\positions.txt"); for (int i = 0; i < 3; i++)
7
6098
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6 --------/--------/--------/--------/--------/--------/ // field_width=8 For example, I position my file pointer at the begining of the 4th fileld lets say 25th character(3*(field_width)+1) and when I try to
8
2667
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five different image files shuffled up in one big binary file. In order to view them I have to "unshuffle" the data, which means moving bytes around. Currently my approach is to read the data from the original, unshuffle as necessary, and then write...
5
5065
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I have it store info it gets from when the programs check in into a DataSet (XML file). Problem is, that file now has to be used by other programs to find out version information (the file is ALWAYS less that 1K.) The record itself is only five fields...
6
5262
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
1648
by: yogesh_anand | last post by:
Hi I am using sun solaris 5.9 operating system.Sometimes after reading from and while writing to file process size used to get increase.(it can be after 20th times 40 th time) I doesn't happen always but some time.After putting print statements i identified the point.Can any body tell me whether it will going to come in memory leak or normal process usuage.If it comes under memory leak how to slove?I have marked the code below from where it...
42
4847
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong. If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data...
0
8459
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
8367
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
8570
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,...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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
4202
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
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
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
1779
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.