need help read data from textfile ^_^

Hello,
I am a beginner in C++ and I am trying to implement of IOSTREAM for reading data in notepad. then I get a problem in displaying the data on the console .

the file ....

example.txt
1
2
3
4
5
6
//------------basic information------// 
No_id : 1.1.0
Name : Rain
Age : 12

activity : sport
drink : water


by example.txt , I want to display in console screen as
1
2
3
4
5
1.1.0
Rain
12
sport
water


and make character of " //" and " ; " become line comment function , such as in c++ .

i hope someone want make documentation for me.
thanks you

sorry for my bad english . :D
Last edited on
You get a problem? What does the error message say? Where is your code?
and make character of" //" and" ; " become line comment function, such as in c++.


What does that mean?
If all your tokens are followed by " : " and continue till a line break then that part is easy. Just read line-by-line and parse data after " : ". If you want to ignore "comments" then check if the first character in the line is a comment delimiter for whole lines comments and I suppose you can also parse the extracted tokenized strings for end-of-line comments.

pseudo

1
2
3
4
5
6
7
8
9
10
11
12
13
open file
  extract line
  parse for comment ("//" or ";")
    is it a comment?
      yes -> deal with it or ignore line
      no -> parse line as data
        find delimiter (" : ")
          found?
            yes -> parse for comment
              comment found?
                yes -> extract data, ignore comment
                no -> extract data to end of line
            no -> error?
Last edited on
@texan >> i got problem on extract line . any example ? thanks you :D
std::getline(myfile, mystring)
i see . and i got problem to find delimeter and parse for comment :D
any idea ? thanks you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt"); 
  if (!myfile)
  {
    cout << "Unable to open file";

  }
   while(myfile){

        getline(myfile,line);
        cout<<line;
    }

   myfile.close();
   cin.get();

  return 0;
}
Let me see... Comment Parsing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//...
{
    getline(myfile,line);
    bool Display = 0;
    char * pointer = line.c_str();
    while(*pointer)
    {
        if(*pointer == ' ' || *pointer == '\t')
        {
            ++pointer;
            continue;
        }
        else if(*pointer == '\\' || *pointer == ';') // Add other values here to add other "delimiters"
        {
            break;
        }
        else
        {
            Display = 1;
            break;
        }
    }
    if(Display) // It's not a comment line
        cout<<line;
}
Last edited on
I think I'd go with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <fstream>
#include <iostream>
#include <cctype>


void trim_comments( std::string & s ) ;

int main () {
  std::string line;
  std::ifstream myfile ("example.txt"); 
  if (!myfile)
  {
       std::cerr << "Unable to open file";
       return 0 ;
  }

  while ( std::getline(myfile, line) )
  {
       trim_comments(line) ;
       if ( line.size() )
           std::cout << line ;
  }

  std::cin.get();
  return 0;
}

void trim_at_delim(std::string& s, std::string delimiter)
{
   std::string::size_type endPos = s.find(delimiter) ;

   if ( endPos != 0 && endPos != std::string::npos )
   {
       // Also remove whitespace between delimiter and previous token
       while ( endPos != 0 && std::isspace(s[endPos]) )
           --endPos ;
   }

   s = s.substr(0, endPos) ;
}

void trim_comments( std::string & s )
{
   static char const* commentDelimiters[] = { "//", ";" } ;

   for ( unsigned i=0;  i<sizeof(commentDelimiters)/sizeof(commentDelimiters[0]); ++i )
       trim_at_delim(s, commentDelimiters[i]) ;
}


This should also give a pretty good idea of what you need to do to get just the portion of the string you need.
Topic archived. No new replies allowed.