?
Solved

Read data from a file

Posted on 1998-10-13
2
Medium Priority
?
177 Views
Last Modified: 2010-04-15
I need to read an entire line of text from a file. How can this be done. I tried to use the fscanf function but this stops after the first word in the first line. For example:
I want to be able to read "This is the first line of text" and store it into a string value.

What I'm wanting to do is add several lines of text to an existing text file. I was going to create a new file with my inserted text at the top of the file and then read from the existing file and print all data that was read to the new file after the newlines.

Hopefully there is a better way...

Thanks
0
Comment
Question by:mirado
[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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 30 total points
ID: 1253497
To read an entire line, use the 'fgets()' function, e.g.

/* FGETS.C: This program uses fgets to display
 * a line from a file on the screen.
 */

#include <stdio.h>

void main( void )
{
   FILE *stream;
   char line[100];

   if( (stream = fopen( "fgets.c", "r" )) != NULL )
   {
      if( fgets( line, 100, stream ) == NULL)
         printf( "fgets error\n" );
      else
         printf( "%s", line);
      fclose( stream );
   }
}

(Example form the VC5 docs on 'fgets()')
0
 

Author Comment

by:mirado
ID: 1253498
Thanks jkr!
0

Featured Post

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.

Question has a verified solution.

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

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…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

770 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