Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading data from file

Tags:

c++

file

I have .txt file which contains data as follows [12,25],[36,45] i.e numbers are enclosed in square brackets separated by comma from each other I want to read such file through C++ program

I referred to string toolkit available,specifically regex facility can be used but i'm not able put in program can someone please help me??

like image 343
username_4567 Avatar asked Oct 16 '11 10:10

username_4567


People also ask

How do I read data from a text file?

To read from a text fileUse the ReadAllText method of the My. Computer. FileSystem object to read the contents of a text file into a string, supplying the path. The following example reads the contents of test.

Which method reads data from file?

Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class. It is character-oriented class which is used for file handling in java.

How do you open a file to read data?

You open a file by passing its filename – e.g. example. txt – into the open() function. The open() function returns a file object. To actually read the contents of a file, you call that file object's read() method.

What is the data type of data read from a file in Python?

Reading from a file There are three ways to read data from a text file. read() : Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.


1 Answers

just use scanf or fscanf like this:

if(scanf("[%d,%d]",&a[i],&b[i])==2){
  ++i;
  while(scanf(",[%d,%d]",&a[i],&b[i])==2) ++i;
}

don't forget that C I/O functions are valid C++.

like image 84
dragon135 Avatar answered Oct 25 '22 01:10

dragon135