473,404 Members | 2,137 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,404 software developers and data experts.

fgets() help

I have a fixed length file that I am reading and trying to process. The
file contains 3 types of records (a,b,c) each has it's own field
definition for the position on the line. Record type is always held in
position 2(php 0,1,2), with a length of 1.

I would like to process each line seperatley, evaluating the record
type, and then taking the appropriate action. Like so:

//open the import file
$import_file = fopen(dirname(__FILE__)."/../../data/imports/data.imp",
"r");

//make sure we can read it
if($import_file)
{
//loop to read each line
while (!feof($import_file))
{
//read line and process accordingly
$line = fgets($import_file);
$record_type = substr($line, 2,1);
if($record_type=="a")
{
//process 'a' types
}
elseif(record_type=='b')
{
//process 'b' types
}
elseif(record_type=='c')
{
//process 'c' types
}
}//end loop
}

I can read the data just fine, my problem is that all records are being
treated as 'a' records. Any help would be greatly appreciated.
Thanks

Jul 17 '05 #1
1 1346
NC
diroddi wrote:

I have a fixed length file that I am reading and trying
to process. The file contains 3 types of records (a,b,c)
each has it's own field definition for the position on
the line. Record type is always held in position 2 (php
0,1,2), with a length of 1. .... I can read the data just fine, my problem is that all
records are being treated as 'a' records.
You omitted "$" in a couple of places:
if($record_type=="a")
The above is a correctly written line.
elseif(record_type=='b')
elseif(record_type=='c')


The two lines above, however, refer to a (probably undefined)
constant record_type, not to the variable $record_type...

An unrelated suggestion: your code can be streamlined like
this:

if($import_file = fopen(dirname(__FILE__) .
"/../../data/imports/data.imp", "r")) {
while ($line = fgets($import_file)) {
$record_type = substr($line, 2,1);
switch ($record_type) {
case 'a':
// Process 'a' record
break;
case 'b':
// Process 'b' record
break;
case 'c':
// Process 'c' record
break;
}
} else {
// Handle file opening error.
}

Cheers,
NC

Jul 17 '05 #2

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

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
10
by: Stuart Anderson | last post by:
I am looking to use Ken Stephenson's CirclePack program (http://www.math.utk.edu/~kens/) with some tiling programs written by Cannon, Floyd, Parry. The programs I want to use are subdivide.c...
2
by: Diego | last post by:
Hi, Using gcc 2.96 This message was suggested by a thread started by Knak on 21/03/04 The question is: When I run the following code, if I want to introduce a second pile of data, the...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
6
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Harry,St....
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to fgets() but also tells how many bytes it read...
24
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...
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
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
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
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
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...
0
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
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...
0
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,...

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.