473,511 Members | 14,951 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 2861

"Dot Kom" <jr*****@hardynet.com> wrote in message
news:5c**************************@posting.google.c om...
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("*",$array_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*****@hardynet.com (Dot Kom) wrote in message
news:<5c**************************@posting.google. 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
9807
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 =...
2
3054
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...
3
9496
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...
2
2003
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...
7
6091
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...
8
2659
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...
5
5056
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...
6
5244
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
1637
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...
42
4821
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...
0
7237
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
7137
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
7349
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
7417
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...
1
7074
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...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
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...
0
4734
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
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.