WRT counter show me that line from a txt file


 
Thread Tools Search this Thread
Top Forums Programming WRT counter show me that line from a txt file
# 1  
Old 02-05-2008
WRT counter show me that line from a txt file

Hello Experts,

I am beginner to C language..

I know I am making a simple mistake but I dont know what the problem here

#include <stdlib.h>
#include <stdio.h>
[code]

int main(void)
{
FILE* fh;
char s[50];
int i;
int count =10;
double a,b;
printf("COUNT= %d", count);
fh=fopen("abc1.txt","r");
if (fh ==NULL){
printf ("Error opening file: abc1.txt");
}

for(i=0; i< count; i++){
fgets(s,49,fh);
if(feof(fh)){
rewind(fh); //fclose(fh);previously doing like this
i--; //fh=fopen("abc1.txt","r");previously doing like this
}
}
sscanf(s,"%lf %lf\n",&a,&b);
printf("VALUES :%f %f\n", a, b );

fclose(fh);
}
[\code]
suppose I ve txt file as below
abc1.txt
[code]
10.32 11.98
24.76 34.87
43 55
6 7
8 9
10 11
12 13
[\code]
I want If the counter value is 10 then it should display me the line 43 and 55 , but here it displays
24.76000 34.87000


I got the error I modified the code in bold .. Thanks..........

Last edited by user_prady; 02-05-2008 at 05:42 AM..
# 2  
Old 02-05-2008
Question

Code:
fh=fopen("abc1.txt","r");
if (fh ==NULL){
printf ("Error opening file: abc1.txt");
}

Is the code compiling alright? From the code you have posted it should dump because of how the input file is being specified specifically the code segment shown above. So how are you specifying the input filename to the compiled executable?

Here's the proper way...

Code:
fh = fopen(*++argv, "r");
if (!fh) {
   printf ("Error opening file: %s\n", *argv);
}

# 3  
Old 02-06-2008
Problem with feof function

Quote:
Originally Posted by shamrock
Code:
fh=fopen("abc1.txt","r");
if (fh ==NULL){
printf ("Error opening file: abc1.txt");
}

Is the code compiling alright? From the code you have posted it should dump because of how the input file is being specified specifically the code segment shown above. So how are you specifying the input filename to the compiled executable?

Here's the proper way...

Code:
fh = fopen(*++argv, "r");
if (!fh) {
   printf ("Error opening file: %s\n", *argv);
}

Thanks for the reply . I ve no problem with that .
I ve abc1.txt in the current directory , Iam not giving from command line argument..

Now I am doing like below for reading a file over and over upto the counter value.. But when I am using feof() func it gives me the following error.

read.c:13: warning: dereferencing `void *' pointer
read.c:13: request for member `_flag' in something not a structure or union

Please help me get my code correct..

Code:
#include <stdio.h>
 
void* fileopen();
void read_line(void*);
 
void read_line(void *fh){
              char s[50];
              int i,j;
              int counter = 3000;
                     for(i = 0; i< counter; i++){
                             fgets(s,49,fh);
                             printf("%s", s);
                             //if(feof(fh)){
                               //      rewind(fh);
                           // }
                     }
                     fclose(fh);
}
void* fileopen(){
          void *file = fopen("abc1.txt", "r");
            return file;
}
int main(void) {
 
           void *fh;
              fh = fileopen();
              read_line(fh);
              return 0;
}

# 4  
Old 02-06-2008
Code:
#include <stdio.h>
 
FILE *fileopen();
void read_line(FILE *);
 
void read_line(FILE *fh)
{
     char s[50];
     int i;
     int counter = 3000;

     for (i = 0; i < counter; i++) {
          fgets(s, 49, fh);
          printf("%s", s);
          if (feof(fh))
               rewind(fh);
     }
     fclose(fh);
}

FILE *fileopen()
{
     return fopen("abc1.txt", "r");
}

int main(void)
{
     FILE *fh;
     fh = fileopen();
     read_line(fh);
     return 0;
}

# 5  
Old 02-06-2008
Quote:
Originally Posted by shamrock
Code:
#include <stdio.h>
 
FILE *fileopen();
void read_line(FILE *);
 
void read_line(FILE *fh)
{
     char s[50];
     int i;
     int counter = 10;

     for (i = 0; i < counter; i++) {
          fgets(s, 49, fh);
          printf("%s", s);
          if (feof(fh))
               rewind(fh);
     }
     fclose(fh);
}

FILE *fileopen()
{
     return fopen("abc1.txt", "r");
}

int main(void)
{
     FILE *fh;
     fh = fileopen();
     read_line(fh);
     return 0;
}

Thank you for your nice reply..

yeah Now it works ,, Can you please Tell me why its printing me the last line of the file twice , In my case if I take
abc1.txt


Code:
12 23
23 34
23 34

Then it prints the last line twice instead of once ..
# 6  
Old 02-06-2008
Bug

Quote:
Originally Posted by shamrock
Code:
#include <stdio.h>
 

void read_line(FILE *);
 
void read_line(FILE *fh)
{
     char s[50];
     int i;
     int counter = 3000;

     for (i = 0; i < counter; i++) {
          fgets(s, 49, fh);
          printf("%s", s);
          if (feof(fh))
               rewind(fh);
     }
     fclose(fh);
}

FILE *fileopen()
{
     return fopen("abc1.txt", "r");
}

int main(void)
{
     FILE *fh;
     fh = fileopen();
     read_line(fh);
     return 0;
}



Actually I want to implement one function like below

It 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 .. If the file pointer fh is already opened in another function fileopen();

Code:
FILE* fileopen(){
          void *file = fopen("abc1.txt", "r");
            return file;
}

void read_line(FILE *fh, double *input_re, double *input_im){
        double a, b;
        char s[50];
        if ( fgets(s,49,fh) != NULL){
          printf("%s ",s);
          sscanf(s,"%lf %lf\n",&a,&b);
          *input_re = a;
          *input_im = b;
        }
        else{
                rewind(fh);
        }
}



Pls help ..
Regards,
User_prady

Last edited by user_prady; 02-06-2008 at 06:28 AM..
# 7  
Old 02-07-2008
Data 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 ..

Last edited by user_prady; 02-07-2008 at 03:25 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switch line in txt file

Hi I have problem with replace line in txt file , I have this string: 144185 DISK Piece qqr8ot6l_1_1 -- 144186 DISK Piece ukr8pf2e_1_1 -- 144187 DISK Piece ter8p9gc_1_1 -- 144188 DISK Piece 4er8qb84_1_1 and (8 Replies)
Discussion started by: primo102
8 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Help on Adding one counter loop at the end of each line in a file

Hello All, I have file a.txt I want to add a counter loop at the end of each line in a file ill explain: i have a site h**p://test.test=Elite#1 i want to add a a counter to the number at the end of the file, that it will be like this urlLink//test.test=Elite#1 urlLink//test.test=Elite#2... (3 Replies)
Discussion started by: nexsus
3 Replies

4. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

5. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

6. Shell Programming and Scripting

Changing Line in Txt File

So I have a python program that I run, which runs accordingly to options I have listed in a text file (ie user_prefs). Now there are many options listed in this user_prefs.txt, but the one of most interest to me is that of the file path of the time series. I have over a hundred of these time... (8 Replies)
Discussion started by: Jimmyd24
8 Replies

7. Shell Programming and Scripting

Count per line in txt file

In a txt file called, eso.txt, I have: ...... 3 where process_status_flag = 70 and LISTENER_ID in (930.00, 931.00, 932.00, 933.00, 934.00) 4 group by LISTENER_ID 5 order by LISTENER_ID; LISTENER COUNT ----------... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

8. Programming

Reading a particular line from a .txt file

Hi, I have a .txt file which contains the x, y and z co-ordinates of particles which I am trying to cast for a particular compound. The no. of particles present is of the order of 2 billion and hence the size of the text file is of the order of a few Gigabytes. The particles have been casted layer... (5 Replies)
Discussion started by: mugga
5 Replies

9. Shell Programming and Scripting

i need to read the last line in a txt file

i'm a beginner in shell and i have a txt file that is updating every second or msec so i need a program to read the last line of this txt file is this possible to do? (5 Replies)
Discussion started by: _-_shadow_-_
5 Replies

10. Shell Programming and Scripting

How to quickly show Nth line from the file

Hi all, How can I quickly show Nth line from the huge file(at least more than 15GB)? I used the following script but seems slower. See 2717298 th line. head -2717298 data0802.dat | tail -1 Thank you very much (4 Replies)
Discussion started by: mr_bold
4 Replies
Login or Register to Ask a Question