?
Solved

Reading a specific line of text from a .txt file

Posted on 2003-03-25
7
Medium Priority
?
293 Views
Last Modified: 2010-04-15
How would I go about reading a specified line from a text file in C? I would be able to use some C++ routines, but im compiling with MSCV++ 1.51 for 16-bit real mode, so I suppose straight C would be the best bet. I suppose I could set the text file with delimiters if I would help...what do you think?
0
Comment
Question by:saivon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:zebada
ID: 8201920
Just read each line until you get to the one that macthes whatever criteria you use to determine if the line is the "specified line" that you want.

#include <stdio.h>

#define MAX_LINE_LEN 1024

int main()
{
  FILE *fd;
  char line[MAX_LINE_LEN+1];
  int  i;

  if ( (fd=fopen("file.txt","r"))==NULL ) {
    perror("file.txt");
    return -1;
  }
  i = 1;
  while ( fgets(line,MAX_LINE_LEN,fd)!=NULL )  {
    printf("Line %d: %s",i,line);
    i++;
  }
  fclose(fd);
  return 0;
}
0
 
LVL 1

Accepted Solution

by:
vamshi_pavan earned 525 total points
ID: 8201923
i have written this small prog.hopefully its of some help to you
#include<stdio.h>
#include<conio.h>

void main()
{
     FILE *fp;
     char a;
     fp=fopen("filename.txt","r");
     a=getc(fp);
          while(a!=EOF)
          {
               printf("%c",a);
               a=getc(fp);
               
          }
          fclose(fp);


}
0
 

Author Comment

by:saivon
ID: 8201978
So - does that mean I could set the text file like this:

[flag1]
data
data
data

[flag2]
data
data
.
.
.

and use GetLine to determine if it is a flag or not, then read the data in line by line? Would I research the file from the start everytime I wanted to find a new flag?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:saivon
ID: 8202024
So - does that mean I could set the text file like this:

[flag1]
data
data
data

[flag2]
data
data
.
.
.

and use GetLine to determine if it is a flag or not, then read the data in line by line? Would I research the file from the start everytime I wanted to find a new flag?
0
 
LVL 1

Expert Comment

by:vamshi_pavan
ID: 8202041
Lets say you have textfile like this

hello everybody,
The global temperature is increasing
everyday at an alarming rate.

The above code would read this file exactly the same way including the spaces,commas and newlines.

As you were asking whether you set the textfile like that above. What you write in your textfile is at your will.Your code  will have to check for these flags and deal with them as you see fit.

0
 

Expert Comment

by:Getch
ID: 8202332
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#define MAXSIZE 512

void main(int argc,char *argv[])
{
     int fp;
     char buffer[MAXSIZE];
     
     if (argc != 1) {
      fprintf(stderr,"usage: %s filename\n",argv[0]);
      exit(-1); }
     if((fp = open(argv[1],O_RDONLY|O_BINARY)) < 0) {
      fprintf(stderr,"I can't open the file %s\n",argv[0]);
      exit(-1); }
     while (read(fd,&buffer,MAXSIZE) > 0) {
            write(1,&buffer,MAXSIZE);
            . // your line goes here
            .
            .
     }
}

getch...
0
 

Author Comment

by:saivon
ID: 8202730
ok thanks guys...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question