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

how to restore the position of a file pointer

I have a file that contains 22 lines, each line has two float separated
by a space.
I want to store those float data into a two-dimensional array which size
I want the program to determine at run time
so i wrote the following function to count the number of lines in the
file to determine the row size for my storage array

int countline(FILE *fp)
{
int count;
count=0;
while( (c=fgetc(fp))!=EOF){
if (c == '\n')
++lcount;
}
return lcount;
}
I know that the file pointer foward one character each time fgetc is called
Is there a way to restore the position of the file pointer to the
beginning of the file
without fclose() and fopen() the file again?
Nov 14 '05 #1
2 4817
in comp.lang.c i read:
Is there a way to restore the position of the file pointer to the
beginning of the file without fclose() and fopen() the file again?


rewind

--
a signature
Nov 14 '05 #2

"Kevin Zhou" <zr*****@hotmail.com> a écrit dans le message de
news:c8**********@parasect.netlab.jp...

Hi,

[snipped]
I know that the file pointer foward one character each time fgetc is called Is there a way to restore the position of the file pointer to the
beginning of the file
without fclose() and fopen() the file again?


Use the functions fseek() or rewind() (in stdio.h). Perhaps this point is a
FAQ, so I invite you to read it.

e.g:

---- try.txt ----
This is the first line of this sample file ...
Here is the second line
Here is the third line
and so forth...

---- exple.c ----
#include <stdio.h>
#include <stdlib.h>

int main(void)
{

int c, retcode = EXIT_SUCCESS;
FILE * fic = fopen("try.txt", "r");

if (fic)
{
while ((c=fgetc(fic))!= EOF)
putchar(c);

putchar('\n');

rewind(fic);
/* fseek(fic, 0L, SEEK_SET); */

while ((c=fgetc(fic))!= EOF)
putchar(c);

fclose(fic);
}
else
{
fprintf(stderr,"Coudn't open try.txt\n");
retcode = EXIT_FAILURE;
}

return retcode;
}

It should output the content of try.txt twice.
Regis.
Nov 14 '05 #3

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

Similar topics

8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
4
by: Rich | last post by:
Hi there, I've been sent a backup file from a SQL Server 2000 DB and tried restoring into a blank DB (with the same name) on my SQL Server 2K, only it errors, see below: (I've separated each...
7
by: Magnus Warker | last post by:
Hi, I want to traverse an (associative) array, starting from its current position, until a certain element is reached. Then, in certain cases, I want to be able to reset the current position of...
2
by: Senthil | last post by:
Hi, The need is to read each line in a text file and based on the read data decision has to be taken whether to reposition the file pointer. The StreamReader.Position does not give current...
1
by: JezB | last post by:
There are a few references on the net about how to restore a page's scroll position over a postback. eg. http://www.devhood.com/messages/message_view-2.aspx?thread_id=104625 My question is : can...
5
by: JezB | last post by:
There are a few references on the net about how to restore a page's scroll position over a postback. This is a simple one which works for me: eg....
0
by: Mirover | last post by:
how can I use the "result set " (fill a temp table) with the returned result set of querys like this : restore filelistonly,restore headeronly...
5
by: Peng Yu | last post by:
Hi, Suppose, I have the following code and the requirement is written along with the code. I'm wondering if there is a way to restore the state of an istream. int n, m; std::ifstream...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.