Reading text file to structures

Hello,
I am trying to learn Cpp language by rewriting my old QBasic and VB programms to Cpp and this goes wery hard.
Here is example which I dont know what and why. So I need help.

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

#include <stdio.h>

struct n_rowdata
{
    char *m_name[27];
    char *m_enable[1];
    char *m_pointer[6];
};

const char *myFile = "names.dat";
int startRecord=1;
int endRecord=startRecord;

int main()
{
    int i;
    FILE *f;
    struct n_rowdata r;

    f=fopen(myFile,"r+");
    if (!f)
        return 1;
    for (i=startRecord;i<=endRecord;i++)
    {
        int sz = sizeof(struct n_rowdata);
    //sz gets 136 instead of 34 bytes??
        fseek(f,sz*i,SEEK_SET);
        fread(&r,sz,1,f);
        printf("%d%s\n",1,r.m_name);
    }
    fclose(f);
    printf("\n");
    return 0;
}


This is basic code similar to those which I use in Basic language. But results are not as expected. I dont know what is wrong.
Here I try to load data from text file into data structure at exact record location (here is startRecord = 1). But length of structure which I gets is 136 instea of expected 34 bytes.
What is wrong here?
Do you want to learn C++ or C? I ask, because your program is almost C-style, you probably have a textbook teaching you C... I recommend you switch your book to some decent C++ textbook.


Anyway, char* m_name[27] is not what you expect. It is an array of 27 different pointers to 27 different character strings. In C, you would just use char m_name[27]; but if you want to learn C++, use std::string m_name; which will be much much much easier to work with. (String handling is probably the most difficult thing for C-beginners.)

Ciao, Imi.
Hello imi, thanks for your reply.
I catch one example on the net and I try to adapt it to my needs. Actually, I dont know on what to begin C or C++. What do you recommend regarding the speed on operating with disk I/O, some IDE, graphics and so. For now I use codeblocks. I succed to compile this example with them (with one warning). How would I know what is C and what C++ looking some example on the net? What is the big (main) difference?
OK, I'm ready to fight with string handling and oher stuff. I have solid expirience with binary IO and string handling in Basic so it will go as it go. Easyer if someone want to help.
Can you please advice me on this exact example how to properly "catch" those data in strings?
I forget to say that I was try to define fixed length strings in my struct with exact numbe of characters. When I changed declarations like std::string m_name; i get many errors during compiling.
This is C++ File IO: http://cplusplus.com/doc/tutorial/files/
And C++ strings: http://cplusplus.com/reference/string/
C/C++ reference (segregated out into it's C and C++ components: http://cplusplus.com/reference/
And the C++ tutorials main page: http://cplusplus.com/reference/
jRaskell, thank you for links.
But I need more concrete help on translating my Basic code to C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Const myFile As String = "names.dat"
Type rowData
        m_name as string * 27
        m_enable as byte    '(one byte long)
        m_pointer as string * 6 
End Type
Dim Shared n_rowData as rowData

ff = Freefile
Open myFile For Binary Access Read Write Shared As ff
For pt = 1 To LOF(ff) / len(n_RowData)
Get #ff, ((pt-1) * len(n_RowData)) + 1, n_rowData
Print n_RowData.m_name; n_RowData.m_enable; n_RowData.m_pointer
Next pt
Close #ff
 

Actually, I dont know on what to begin C or C++.

My recommendation is to learn C++.

My personal feeling is, that it is quicker for beginners to grasp and apply the concepts of good C++ than of good C. Be aware though, that there are also a LOT more concepts in C++. You will need a very long time until you really feel comfortable thinking in C++ - compared to C. (And since C++ is so huge compared to C, the phrase "thinking in C++" is rather empty anyway).

When it comes to performance, I don't think there is a real difference anyway (especially when you come from Basic *g*).



For your current example, the "way in C++" is using abstraction types for the different elements. You use "string" which comes with his own functions to handle strings instead of raw character memory plus utility functions.


You use streams (cout and cin) to handle input and output streaming which also come with their own functions (e.g. cout << stuff) instead of using plain memory handles that you pass around utility functions (C-ish style).


And you don't structure a function into parts "initialize all variables, calculate stuff using variables, return the result" as it is common in C or Pascal (and QBasic?) but the difference between variable initialization and calulation starts to blur (you use a lot of temporary variables and pass results of functions directly to other functions or return them directly). Most visible is this by the fact that usually in C++ you don't declare a variable until it's really used.


To learn C++, I recommend Stroustrup, "The C++ Programming Language". It's precise and it is a very good reference to have around you. And since you already know a lot of the basic concepts (loops, variables, functions etc) you don't need a day-nanny book, right? ;-)


Ciao, Imi.
Hello imi, thanks for your reply.
For learn something different people requires different methods for reach some "fast result".
What mean learn if not "learn to practically use".
So I comme here to try to learn on my allready maded examples in Basic what I see now was not good idea and I should try to find a place where this would be possible, if place like that exists.
This is my first snippet in C++ language and regarding performance I dont see any difference from results which I get with Basic'c code. Except in more complicated coding. True, I dont understand yet scope of variables, calling functions, manipulating strings and all of that what I understand good when Basic is thema.
My main intention is to move from MS platform and their technology, also from their new Basic (.net).
Btw, in Basic is also possible to write good, short and efficient code and bad code too, which both have a same function :)
I understand what you mean for "thnking in C++" and I know which way is needed to reach this. Long way! I belive C++ is "huge" tool so I will try to pick only things which is needed by certain projects with avoiding to learn "all".
Of course, if I find forum for beginners suitable to my needs.

Thank you for your literature recommendation.
nime.
Topic archived. No new replies allowed.