Reade first character of each row file txt

Hi, i'm new on this forum and on c++. My target is to read a input file of this type:

9 10.1.1.9 10.1.1.1 3,500000 00:00:00:00:09 00:00:00:00:08 7

and do this: ONLY IF the first character of the row is "9" i take and extract the number write in bold, in this example 3,500000. Now i have to write all in a new file.

Another curiosity is how i insert, each time that i read a row that have the number in bold that have three zero at the end XX,XXX000, a new line before this row.
My partial code is this, can you help me!
Thank's..
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
    
    #include<iostream>
    #include<fstream>
    #include <sstream>
    using namespace std;

    int main()
    {     

    string buffer;
    ofstream out;
    ifstream in1;
    out.open("out-ciclo.txt");
    in1.open("in-ciclo.txt");

    int countline(1);

    while(getline(in1,buffer))
    {           
             
    ...............       
    ...............
    ...............           
    }

    in1.close();
    countline=0;
    buffer.clear();

    }
That could be tricky, because the fstream objects don't differentiate between bold vs not bold (as far as I know, they read unformatted ASCII characters).

If you have some other way of indicating a number you want to extract, then try using getline(ifstream, /*std::string*/); to store an entire line in a string, and if the first character is a 9, do something, else clear the string and repeat.

-Albatross
No, the bold is only to underline what characters i have to analyse. In the input file there's not bold character.. Can you give an example of code to do what you say?
Thank's

Infact my problem is how to indicate the first character is 9.. What are the commands?
closed account (zb0S216C)
You could do this:
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
#include <fstream>
#include <stdio.h>

bool ReadFile( const char *p_pFile = NULL )
{
    if( p_pFile == NULL )
    {
        return false;
    }

    // Open the specified file for reading.
    FILE *pFile( fopen( p_pFile, "r" ) );
    if( pFile == NULL )
    {
        return false;
    }
  
    // Buffers.
    unsigned uStartNumber( 0 );
    unsigned uTargetNumbers[ 2 ];
 
    // Extract the numbers we want and discard those we don't.
    fscanf( pFile, "%u %*u.%*u.%*u.%*u %*u.%*u.%*u.%*u %u,%u", &uStartNumber, &uTargetNumbers[ 0 ], &uTargetNumbers[ 1 ] );

    // Evaluate the extracted data.
    if( uStartNumber == 9 )
    {
        // Do stuff.
    }

    // Print the values we have extracted( Optional ).
    printf( "%u, %u, %u.\n", uStartNumber, uTargetNumbers[ 0 ], uTargetNumbers[ 1 ] );
 
    // Close the file now that we have done with it.
    fclose( pFile );
    pFile = NULL;

    return true;
}


If this is your homework then make sure you learn from it, not copy it.
Last edited on
Topic archived. No new replies allowed.