What is the best way to read a text file and print to console, then grab that string and convert string to a const char?

Recommended Answers

All 12 Replies

my favorite way is using getline(ifstream, string); and then i would just push everything on to a stack of type const char. you could also use vector, list, queue depending on what you need to use it for

Do you mean const char array?

if it's const char array then you won't be able to add text from a text file to it. But maybe what you want is a function whose parameter is const char*?

I need to input the results of a text file to my console.

Then I need to get the length of characters in that text file by using the "gets(text)" and converting the "text" to int like this,

"this is from the text file let's say"
.......
...
..
gets(text);
textLength = strlen(text);

how would i start?

sorry i wasnt thinking. it would have to be first a char array or container. then you could make a const char array or container later equal to the char whatever

That's the gets() that all the C docs warns against using?

From my manpages...

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

First, gets() gets text from the keyboard, not from a data file. What you want is fgets() to read the text file. Do you know how to read text files? If not, here is a short tutorial

Okay, so i went ahead and use the fgets to print data from my text file,

                static const char filename[] = "test.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
      char line [ 128 ]; /* or other suitable maximum line size */

      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         fputs ( line, stdout ); /* write the line */
      }
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }

How do i get the length of characters of the data from the text file?

Reason is because I need to compare the length of characters to other thigns later on in the program.

after line 9 add another line to call strlen() to get the length of the line just read. If you want all the lines in the file then add them up into an int variable that you can use later in the program.

[

int sum = 0;
while( fgets(...) ) // replace ... with actual parameters
{
    sum += strlen(text);
}

](null)

I would use a running total getting the strlen of line.

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>

#define TEXTMAX 999999//maximum text length
#define WORDMAX 20//maximum word length

void main()
{

int textLength=0;

static const char filename[] = "test.txt";
       FILE *file = fopen ( filename, "r" );
       if ( file != NULL )
       {
          char line [ 128 ]; /* or other suitable maximum line size */



      while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
      {
         fputs ( line, stdout ); /* write the line */
      }
      fclose ( file );
   }
   else
   {
      perror ( filename ); /* why didn't the file open? */
   }


textLength = strlen(charactersFromTextFile);

....
....
......


}
Member Avatar for iamthwee

What is the best way to read a text file and print to console, then grab that string and convert string to a const char

Let me restore some sanity to this thread. Use std::strings, we're using c++ yes? That would also negate the need to convert it to a const char.

There are lots of tutorials regarding how to read strings in c++ and reading data from a file, you can search for them on Daniweb.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.