how to format data

Pages: 12
Hi guys,
I'm not very expert but I should create a program in C that reads the data contained in an external text file about eeg signals and reprint them but in order and tabulated in another file to have the data arranged more correctly ( for example like this

1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI

I would like to use the function strcmp...
Can anyone give me some advices?
Thank you
Last edited on
This should give you an introduction to how to read from and write to files:

http://www.cplusplus.com/doc/tutorial/files/
Thank you, I know that I have to read from this .txt file but I don't know then how to continue. Shoul I create a vector that reads every single line?
The data are in a file like this:
Sesión 4 (ONLINE FREE MODE):

CORRIDA 1:
Número de secuencias: 7

CARACTERES A DELETREAR: TORTUGA

So i should tabulate the number of sessions, sequences and characters
Is this C Or c++? C does not have vectors.

strcmp is fine but you may or may not need to uppercase/lowercase the data to use it for sorting it depending on the data in the file and how you want the output: 'a' and 'A' are very different numbers when looking at them as a sorting criteria.

C has a sort() routine but I forget how it works, should be easy to find an example.
it looks to me like you need a C-struct of the data in a line of the file, a sort of that data using the built in routine and a comparison function on your struct, and an output function.
Last edited on
sorry, I meant in c ++. I thought to have the file read up to certain keywords and then the numbers that come after associating them with arrays and tabulating them in an orderly way. But I don't know if am i right ...
Last edited on
If your data is exactly as you've shown then the simplest way to read it is something like 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream in("file.txt");

    int nSession = 0, nCorrida = 0, nSecuencias = 0;
    string caracteres, word;

    in >> word;
    in >> nSession;
    in >> word >> word >> word >> word;
    in >> nCorrida;
    in >> word >> word >> word >> word; // is there really a : after the 1 ?
    in >> nSecuencias;
    in >> word >> word >> word;
    in >> caracteres;
    
    cout << nSession    << '\n'
         << nCorrida    << '\n'
         << nSecuencias << '\n'
         << caracteres  << '\n';
}


Thank you very much, I think that's the right way to begin! The file is not exactly imposted like that one i sent before because there are other informations that I don't have to care about for my program. Am I asking why they told me to use the strcmp function (?) To order the data information right? To write the new data collected ordinarly into a new file .txt I think I should use list functions
Last edited on
strcmp is a C function, you should not use it in c++.
c++ strings can be directly compared using > < == type operations.
strcmp provides the above comparison operations via a function to the C language.
C++ can use most of C's tools, but has better alternatives.

I think you need to read it into a container, sort it, and write it back out same as I suggested in C, just using C++ implementation. The logic is the same.
Last edited on
Thank you jonnin,
I'm using visual studio. What if a read only certain parts of the file that I need like dutch suggested and then use array to sort data in order?
Sorry dutch if can I ask you something: your way is good but it seems to be very long until I have to measure each word and number...Is there a shorter way?
I have then to order these elements in a new file .txt
Maybe something like this. For anything more specific you need to specify exactly what your input looks like and exactly what output you want.

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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int get_next_integer(istream& in) {
    string word;
    int n = 0;
    while (in >> word && (word.empty() || !isdigit(word[0])))
        ;
    if (in) n = stoi(word);
    return n;
}

string next_word_after_colon(istream& in) {
    string word;
    while (in >> word && (word.empty() || word.back() != ':'))
        ;
    if (in) in >> word;
    return in ? word : "";
}

int main() {
    for (int i = 0; i < 3; ++i) {
        int n = get_next_integer(cin);
        if (cin)
            cout << n << '\n';
    }
    string word = next_word_after_colon(cin);
    if (cin)
        cout << word << '\n';
}

Thank you dutch,
the input file is like this:
Sesión 1 (COPY MODE):
Número de secuencias: 15

CORRIDA 1:
CARACTERES A DELETREAR : CALOR

CORRIDA 2:
CARACTERES A DELETREAR : CARINO

CORRIDA 3:
CARACTERES A DELETREAR : SUSHI

Sesión 2 (COPY MODE):
CORRIDA 1:
Número de secuencias: 15

CARACTERES A DELETREAR: SUSHI
CARACTERES RECONOCIDOS: SUSHI

_____________________________________________
Sesión 3 (ONLINE FREE MODE):
Número de secuencias: 15

CORRIDA 1:

CARACTERES A DELETREAR: CENAR
CARACTERES RECONOCIDOS: CENAR

CORRIDA 2:

CARACTERES A DELETREAR: COLOR
CARACTERES RECONOCIDOS: COLOR

CORRIDA 3:

CARACTERES A DELETREAR: DULCES
CARACTERES RECONOCIDOS: DULCES


And the output file should be like this:
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI

So I have to read only certain parts of the input file, store them into arrays and sort them in ascending order just in function of the sesion...I hope you can help me! I really don't know how to do this...Thank you
Last edited on
I hope you can help me

I can't help because I don't understand how that output came from that input.
Nothing makes any sense to me at all.
mh, if you see in this output recognizes only certain data, not all the file:
the important thing is the the numbers along the column are in order from 1 to... until the session are finished
I don't know how to read only some data, maybe to recognize keywords

sesion corrida sequencias deletrear
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI
The first way you suggested me is a good way to begin but the files have to ensure that they are written to a new file and sorted by row not by column, as in the example I showed you. I don't know if I explained myself, thank you anyway for the availability
so the file is in 3 sections but how does the 15 relate to all 3 items? What tells me that in the input?
the number 15 for example is related to secuencias in the input file, I have to read only parts of that file non everything of what if contains
Last edited on
Please give the precise output that corresponds to a particular input.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


struct Item
{
   int sesion;
   int corrida;
   int secuencias;
   string deletrear;
};


bool sortBy( const Item &a, const Item &b )
{
   if ( a.sesion < b.sesion ) return true;
   if ( a.sesion > b.sesion ) return false;
   return a.corrida < b.corrida;
}


int main()
{
// ofstream out( "output.dat" );
   ostream &out = cout;

// ifstream in( "input.dat" );
   istringstream in( "Sesión 1 (COPY MODE):           \n"
                     "Número de secuencias: 15        \n"
                     "                                \n"
                     "CORRIDA 1:                      \n"
                     "CARACTERES A DELETREAR : CALOR  \n"
                     "                                \n"
                     "CORRIDA 2:                      \n"
                     "CARACTERES A DELETREAR : CARINO \n"
                     "                                \n"
                     "CORRIDA 3:                      \n"
                     "CARACTERES A DELETREAR : SUSHI  \n"
                     "                                \n"
                     "Sesión 2 (COPY MODE):           \n"
                     "CORRIDA 1:                      \n"
                     "Número de secuencias: 15        \n"
                     "                                \n"
                     "CARACTERES A DELETREAR: SUSHI   \n"
                     "CARACTERES RECONOCIDOS: SUSHI   \n"
                     "                                \n"
                     "Sesión 3 (ONLINE FREE MODE):    \n"
                     "Número de secuencias: 15        \n"
                     "                                \n"
                     "CORRIDA 1:                      \n"
                     "                                \n"
                     "CARACTERES A DELETREAR: CENAR   \n"
                     "CARACTERES RECONOCIDOS: CENAR   \n"
                     "                                \n"
                     "CORRIDA 2:                      \n"
                     "                                \n"
                     "CARACTERES A DELETREAR: COLOR   \n"
                     "CARACTERES RECONOCIDOS: COLOR   \n"
                     "                                \n"
                     "CORRIDA 3:                      \n"
                     "                                \n"
                     "CARACTERES A DELETREAR: DULCES  \n"
                     "CARACTERES RECONOCIDOS: DULCES  \n" );

   int sesion = 0, secuencias = 0, corrida = 0;
   string deletrear, line, dummy;
   vector<Item> items;

   while ( getline( in, line ) )
   {
      if ( line.find( "Sesión" ) != string::npos )
      {
         stringstream ss( line );
         ss >> dummy >> sesion;
      }

      else if ( line.find( "secuencias" ) != string::npos )
      {
         stringstream ss( line );
         getline( ss, line, ':' );
         ss >> secuencias;
      }

      else if ( line.find( "CORRIDA" ) != string::npos )
      {
         stringstream ss( line );
         ss >> dummy >> corrida;
      }

      else if ( line.find( "DELETREAR" ) != string::npos )
      {
         stringstream ss( line );
         getline( ss, line, ':' );
         ss >> deletrear;
         items.push_back( { sesion, corrida, secuencias, deletrear } );
      }
   }

   sort( items.begin(), items.end(), sortBy );

   for ( const auto &e : items ) out << e.sesion << '\t' << e.corrida << '\t' << e.secuencias << '\t' << e.deletrear << '\n';
}


1	1	15	CALOR
1	2	15	CARINO
1	3	15	SUSHI
2	1	15	SUSHI
3	1	15	CENAR
3	2	15	COLOR
3	3	15	DULCES
Last edited on
Thank you, thank you for your availability!
One thing: what if the program reads from an input file these characters?
Maybe I shoud use just the second part of the program and modify the first one in order to read the external file that contains the data
Okay I tried to modify the first part and now it reads from an input file, now I just have to print the results in another file but I'll try on my own...
Thank you so much

I'll have to study this, so can I ask you something if I don't understand it?
Pages: 12