473,771 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file operation

Hi,

I have following in my data file:

<12345><Benjami n><Single>
<4567><Stephen> <Married>
<8383245><Georg e><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

.... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buf fer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}
Any hints/tips will be great. Thanks.
Nov 14 '05 #1
5 1500
"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
I have following in my data file:

<12345><Benjami n><Single>
<4567><Stephen> <Married>
<8383245><Georg e><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single
It would perhaps be better to make DisplayInfo(0) return the first line. Its
up to you.

[snip] #define LINE_SIZE 100;
static char buffer[LINE_SIZE];
Why do you use a file-scope buffer rather than moving this (minus the
'static') into the function?
void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buf fer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}

Any hints/tips will be great. Thanks.


Once you've opened the file, to get to the start of line linenum, you should
discard (linenum - 1) lines (with the first line being numbered 1). This is
equivalent to reading and discarding everything up to and including the
(linenum - 1)th '\n', which you can easily do using getc() or fgetc() in a
loop. Don't forget to check for EOF.

After the above, you are in position to read the desired line. You can use
scanf(), or fgets() followed by sscanf() (preferred), to read and convert
the fields into suitably-typed variables. Or you can continue using
getc()/fgetc(): skip until you see '<', then write each character you read
until you see '>' (this displays the number), and similarly for the other
fields. Again, don't forget error checking.

Alex
Nov 14 '05 #2

"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

I have following in my data file:

<12345><Benjami n><Single>
<4567><Stephen> <Married>
<8383245><Georg e><Married>

I want to write a function that display according to the line number given
(Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buf fer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}
Any hints/tips will be great. Thanks.


or how can I set the file pointer to beginning of line num "linenum" ?
Nov 14 '05 #3

"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...

"Magix" <ma***@asia.com > wrote in message
news:41******** **@news.tm.net. my...
Hi,

I have following in my data file:

<12345><Benjami n><Single>
<4567><Stephen> <Married>
<8383245><Georg e><Married>

I want to write a function that display according to the line number given (Max line number is 5), without the '<' and '>'
Example:
DisplayInfo(1) will display:-

12345 Benjamin
Single

DisplayInfo(2) will display:-

4567 Stephen
Married

... and so on.

#define LINE_SIZE 100;
static char buffer[LINE_SIZE];

void DisplayInfo(int linenum)
{
//... file open here, if error, return.
while(fgets(buf fer, sizeof(buffer), fp)!=NULL)
{
// check for line number
}
fclose(fp);
}
Any hints/tips will be great. Thanks.


or how can I set the file pointer to beginning of line num "linenum" ?


Each time you call 'fgets()', the 'file pointer' will point to
the character immediately following a newline ('\n'). So just
loop until you've called 'fgets()' 'linenum' times. (Checking
for errors and end-of-file as you go.)

unsigned int line_count = 0;
while(fgets(buf fer, sizeof buffer, fp)
printf("%u line(s) read\n", ++line_count;

-Mike
Nov 14 '05 #4
"Mike Wahler" <mk******@mkwah ler.net> writes:
[...]
Each time you call 'fgets()', the 'file pointer' will point to
the character immediately following a newline ('\n'). So just
loop until you've called 'fgets()' 'linenum' times. (Checking
for errors and end-of-file as you go.)


*Unless* the input line is longer than the buffer.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Keith Thompson wrote:
"Mike Wahler" <mk******@mkwah ler.net> writes:
[...]
Each time you call 'fgets()', the 'file pointer' will point to
the character immediately following a newline ('\n'). So just
loop until you've called 'fgets()' 'linenum' times. (Checking
for errors and end-of-file as you go.)

*Unless* the input line is longer than the buffer.


The three cases for fgets():

1. Returns a complete line with \n\0.
2. Returns NULL (End of file or error).
3. Returns a short line. Either buffer too short or line w/o '\n'.

It is this third case that is of greatest interest. :-)

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #6

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

Similar topics

0
1346
by: Andrius | last post by:
Hello, having some problems by writing XML file by XML schema. using strongly typed datasets. Schema: <?xml version="1.0" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:include schemaLocation="types.xsd" />
0
1953
by: Dave | last post by:
Hi, If anyone could help with this, I would greatly appreciate it! I've created my own WSDL file and referenced an XSD file too. I've generated a service side interface using wsdl /server, and created an ASMX file from this. I've then set the Binding for the class and methods and set the Location to my WSDL file. If I put the wsdl:service element in the WSDL file that points to my endpoint, I can go to Add Web Reference and my methods...
0
1477
by: suresh | last post by:
Hello All, Iam writing a wsdl file using the Servicedescription class.The document I get is a well formed xml document.But some how when I try to add this wsdl file as a webrefernce to another project in VisualStudio.NET it gives me the following error " c:\inetpub\wwwroot\WebApplication3\Web References\WebReference\Reference.map(1): Custom tool error: Unable to import WebService/Schema. Cannot find
1
2296
by: moonriver | last post by:
In a xml file, can we make reference to another xml file so that all contents of the latter xml file will be included into the first xml file? Had better give me an example for details.
0
3940
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
1
4522
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum performance impact on any read operations to that file - a common task right.... in the unix world the way to do this is to create a temporary file with
10
5952
by: vincex200 | last post by:
My group needs help with this program. We attempted to start it and got no where. Please help us. Write a C++ program that will read data from a file, perform computation on the data, then print the result to another file. The data from the file will contian 100 lines where each line contains three items. The three items are an arithmetic operation followed by two integer values (non zero.) The program should perform the arithmetic operation...
13
1827
by: Rajkiran R.B. | last post by:
I have written a program in C#... Now the requirement is that I want an assembly code to be the output rather than the regular executable output.. Can anyone say how to do it..
1
1793
by: Primo | last post by:
I have 3 files and I want all of them to go to the bank and one of them will go to both the bank and a vendor. this is a code we have: <process name="FD_Generic_Outbound_banksplit"> <sequence name="Sequence Start"> <assign to="SetExtractionArchiveFolder" from="'..inbound'"></assign>
0
9619
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
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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
9911
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...
1
7460
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
6713
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();...
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.