answersLogoWhite

0


Best Answer

Text files do not contain int types, only char types. Alternating between the two when reading a text file is an exercise in futility.

The assumption here is that the text file contains integers in string form. To extract the integers you must convert the digits into values. The digit '4' is not a number; four is the number, 4 is just the symbol for the number four. However, '4' is ASCII character code 52, thus if we subtract 48 we get its value. Character '0' is ASCII 48, thus subtracting '0' is the same as subtracting 48. If 0 <= value <= 9, then we know the character is a digit.

The following example demonstrates how to extract integers from strings. The code can be easily adapted to extract integers from text files.

#include <iostream>

int main()

{

char str[] = "This is a string that has 54 characters and 2 numbers.";

std::cout << "'" << str << "'\n" << std::endl;

int idx = 0;

again:

int number = 0;

while( str[idx] )

{

char value = str[idx++] - '0';

if( value >= 0 && value <= 9 )

{

number *= 10;

number += value;

}

else if( number )

break;

}

if( number )

std::cout << "The number " << number << " was found in the string." << std::endl;

if( str[idx] )

goto again;

return( 0 );

}

Output:

'This is a string that has 54 characters and 2 numbers.'

The number 54 was found in the string.

The number 2 was found in the string.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

#include<iostream>

#include<fstream>

#include<string>

std::string load_text( std::ifstream& in )

{

std::string result;

// we'll read 4k at a time

const size_t bufsize = 4096;

// intialise a buffer (include a null-terminator)

char buffer[bufsize+1];

memset( buffer, 0, bufsize+1 );

while( in.good() )

{

// read characters

in.read( buffer, bufsize );

// append to result

result.append( buffer );

// reset buffer for next read

memset( buffer, 0, bufsize );

}

return( result );

}

int main ()

{

// assign a filename to an input file stream

std::ifstream in( "c:\\readme.txt" );

// ensure the file is good

if( in.bad() )

return( -1 );

// load the text into a string

std::string str = load_text( in );

// finished with file

in.close();

// output the string

std::cout << str.c_str() << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Here is a simple program that saves the contents of a file to a string:

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

void main()

{

string VariableName;

ifstream infile;

infile.open("TextFileHere.txt");

while(!infile.eof) //Gets all the lines

{

getline(infile,VariableName); //Saves the string to the variable

cout << VariableName; //Outputs the variable to the console

}

infile.close();

system("pause");

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Use an input file stream (ifstream) object to read a text file.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you alternate between reading char and int in C plus plus from a text file?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which data type is used in order to fetch stream of data from network or file?

char


Write a program to find the reverse of a string in c using function?

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void reverse(FILE * file) { int fscanf_return_value; char x; /* read a char */ fscanf_return_value = fscanf(file,"%c",&amp;x) ; if(fscanf_return_value == EOF) { //fscanf returns EOF as the END OF FILE return; } reverse(file); //Get the next char //do something with the char e.g. print putchar(x); return; } int main(int argc, char *argv[]) { int i; FILE *fd; if (argc!=2) { printf("Usage : \n %s FILENAME\n",argv[0]); exit(0); } if(!(fd=fopen(argv[1],"r"))) { printf("Opening file error\n"); exit(1); } reverse(fd); printf("\n\n\t---\tenoD - Done\t---\n"); close(fd); exit(0); } .....................if u like it get ask me more my FB ID is phani.9885120096@gmail.com.


How do you read and write video file in java?

you can use inputstream for reading the file java.io.fileinputstream and write the file using outputstream..


C plus plus code to read the data from any file?

The following function will read any valid file name one byte at a time. int read_file(char* filename) { FILE* input=fopen( filename,"rb" ); if( !input ) { std::cerr&lt;&lt;"File access error: "&lt;&lt;filename&lt;&lt;std::endl; return( -1 ); } char ch; while( fread( &amp;ch, 1, 1, input )==1 ) { // process the byte... } fclose( input ); return( 0 ); }


To read data which is superior 1. input stream 2. file reader?

First of all, these two classes are on different levels of abstraction. An InputStream is used for reading any stream of bytes, while a FileReader is used to read characters from a file. If you want to ask between a FileInputStream and a FileReader, then we need to look at what type of data you are reading. If you're reading plain-text file, for example, you want to use a FileReader because it was designed to read in characters. For other types of data, the FileInputStream would be better, as it is used to read in generic streams of bytes from a file.

Related questions

How do you get your Minecraft skin onto your PC in a PNG file?

gotta get iexplorer to do that download it for free then download a skin then overwrite the char file


Which data type is used in order to fetch stream of data from network or file?

char


Can you use rubber ninjas custom campaign stuff like weapons in two player?

Yes you can. All you have to do is: if its a character you have to move the .char file and the .ragd file over to the character folder and the ragdoll folder. Then in order to play with them in two player mode you have to go to the 2playersettings.txt and type in the char file.


What are hardlinks and softlinks used for in Linux?

Alternate references to a file/directory.


Where to download Gundam Seed DS?

You don't, because that's pirating intellectual property. And it's illegal.


Write a program to find the reverse of a string in c using function?

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void reverse(FILE * file) { int fscanf_return_value; char x; /* read a char */ fscanf_return_value = fscanf(file,"%c",&amp;x) ; if(fscanf_return_value == EOF) { //fscanf returns EOF as the END OF FILE return; } reverse(file); //Get the next char //do something with the char e.g. print putchar(x); return; } int main(int argc, char *argv[]) { int i; FILE *fd; if (argc!=2) { printf("Usage : \n %s FILENAME\n",argv[0]); exit(0); } if(!(fd=fopen(argv[1],"r"))) { printf("Opening file error\n"); exit(1); } reverse(fd); printf("\n\n\t---\tenoD - Done\t---\n"); close(fd); exit(0); } .....................if u like it get ask me more my FB ID is phani.9885120096@gmail.com.


Can you read a file after created by a program in C plus plus?

Yes. You can either create a file for both reading and writing, or you can re-open a file for reading after creating and writing to it.


How many arguments does a fgetsfunction require?

Use the manual/help.char *fgets (char *s, int size, FILE *stream);


Reading file information on removable cds or disks is made possible by the?

Reading Sensors..


How do you define a function?

Put a function prototype in a header file or before the function is called in a C source file. void foo(void); or int bar(int,float char*);


Why only three types of pointer variable in c language?

I have no idea what you mean by that... Some examples for pointers of different types: int *intptr; char *charptr; void *generic_ptr; FILE *stdin; int (*funptr)(int, char **);


How do you add endings for characters in mugen?

first... you get tha ending file (def, sff and mp3 or midi, etc)now you open your charactr DEF, look for arcadeand write the name of the ending file for example....introstoryboard =endingstoryboard = data/capend/ending.defy put the espeificationof the ending file if you need to put the ending for more than 1 char..... or put the ending file into the character carpet if the ending is just for this char like this....endingstoryboard = ending.def