answersLogoWhite

0

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

12y ago

What else can I help you with?

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.


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

Reading Sensors..


How many arguments does a fgetsfunction require?

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


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*);


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..


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 **);