Sponsored Content
Top Forums Programming Problem with fgets and rewind function .. Post 302165190 by user_prady on Thursday 7th of February 2008 02:27:13 AM
Old 02-07-2008
Problem with fgets and rewind function ..

Hello Friends,

I got stuck with fgets () & rewind() function .. Please need help..

Actually I am doing a like,
The function should read lines from a txt file until the function is called..
If the data from the txt file ends then it goes to the top and then again when the function is called it should display me the top row .. I am doing like below , But it's not doing what I am expecting

Please need help..

Code:
#include <stdio.h>
 
FILE* fileopen();
void read_line(void*);
 
void read_line(void *fh){
              char s[50];
              int i,n;
                            if( fgets(s,49,fh) != NULL) {
                                n = 0;
                                    while(isspace(s[n])){
                                        n++;
                                        if(s[n] == '/' && s[n++] == '/'){
                                                fgets(s,49,fh);
                                }
                             }
                                      printf("%s", s);
                            }
                            else{
                                    rewind(fh);
                            }
}
FILE* fileopen(){
          void *file = fopen("abc1.txt", "r");
            return file;
}
int main(void) {
           void *fh;
            int i;
              fh = fileopen();
               for(i = 0;i < 12; i++){
                 read_line(fh);
                }
              return 0;
}

abc1.txt
Code:
6  7
8 9
12 13

output
Code:
6  7
8 9
12 13
6  7
8 9
12 13
6  7
8 9
12 13

It should give me 12 rows as the function is called 12 times..
but it is returning me 9 rows..

How to solve it I am wondering from last couple of days ..Pls need help badly..

Regards,
prady
 

10 More Discussions You Might Find Interesting

1. Programming

rexec() function problem

Hi folks, I'm trying to make a reconnection algorithm using rexec(), but I noticed that when rexec() fails returning -1, it is impossible to make it run successfully again until you restart the program or the thread. Example, I have a endless loop for connection retries, if I supply a wrong... (7 Replies)
Discussion started by: lcmoreno
7 Replies

2. Programming

fgets()

does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it... (4 Replies)
Discussion started by: skanky
4 Replies

3. UNIX for Dummies Questions & Answers

What is the function of rewind()?

What is the function of rewind()? (2 Replies)
Discussion started by: tigerkin
2 Replies

4. UNIX for Dummies Questions & Answers

Need help to understand cpio and no rewind tapes

SCO openserver 5r5 I only have this available to me ... To list the files... cpio -itcvB < /dev/nrct0 To copy a file out cpio -icvdBum filename < /dev/nrct0So cpio is to archive or "zip" files up?? and /dev/nrct0 is the tape drive ??? How can i list all the files inside... (2 Replies)
Discussion started by: khaos83_2000
2 Replies

5. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

6. Programming

[C] fgets problem with SIGINT singlal!!!

Hi all, I have this method to read a string from a STDIN: void readLine(char* inputBuffer){ fgets (inputBuffer, MAX_LINE, stdin); fflush(stdin); /* remove '\n' char from string */ if(strlen(inputBuffer) != 0) inputBuffer = '\0'; } All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies

7. Programming

fgets problems

I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake? This is the minimal code needed to cause the error for me: FILE *f = fopen(name, "r"); if (!f) return; pari_sp ltop = avma; char line; while(fgets(line, 1100, f) != NULL) printf(".");... (23 Replies)
Discussion started by: CRGreathouse
23 Replies

8. Programming

fgets problems newline

hello, i'm trying to write a C-program that reads a file line by line. (and searches each line for a given string) This file is an special ASCII-database-file, with a lot of entries. I checked the line with most length, and it was about 4000 characters. With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies

9. Programming

How do I copy or rewind *argv[]

I'm working on my own pow function and I need to make a copy of *argv but I think that I am having trouble with the size of *argv and the size of any array that I make. The code below isn't working for me. and I want to accept any number no matter the size with pow -f 2 2. I was working out... (16 Replies)
Discussion started by: Errigour
16 Replies

10. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies
FGETS(P)						     POSIX Programmer's Manual							  FGETS(P)

NAME
fgets - get a string from a stream SYNOPSIS
#include <stdio.h> char *fgets(char *restrict s, int n, FILE *restrict stream); DESCRIPTION
The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte. The fgets() function may mark the st_atime field of the file associated with stream for update. The st_atime field shall be marked for update by the first successful execution of fgetc(), fgets(), fgetwc(), fgetws(), fread(), fscanf(), getc(), getchar(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc() or ungetwc(). RETURN VALUE
Upon successful completion, fgets() shall return s. If the stream is at end-of-file, the end-of-file indicator for the stream shall be set and fgets() shall return a null pointer. If a read error occurs, the error indicator for the stream shall be set, fgets() shall return a null pointer, and shall set errno to indicate the error. ERRORS
Refer to fgetc() . The following sections are informative. EXAMPLES
Reading Input The following example uses fgets() to read each line of input. {LINE_MAX}, which defines the maximum size of the input line, is defined in the <limits.h> header. #include <stdio.h> ... char line[LINE_MAX]; ... while (fgets(line, LINE_MAX, fp) != NULL) { ... } ... APPLICATION USAGE
None. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
fopen() , fread() , gets() , the Base Definitions volume of IEEE Std 1003.1-2001, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 FGETS(P)
All times are GMT -4. The time now is 10:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy