?
Solved

Reading a specific line of text from a .txt file

Posted on 2003-03-25
7
Medium Priority
?
297 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

571 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