Read data from text file

Hey guys, currently i'm doing my project but i have stuck on some part.
I have 1 text file with have 3000 line which have save length of data,
From that text file, my program should read 10 line and send to client for process part then continue for next 10 line until end line of text file.

My problem is, i only manage to send first 10 line only for process and then it repeat again and again.

here my code:
[char Mosaic(char* &message)
{
ifstream text("Data.txt");
if (text.is_open())
{
for (int count = 0; count < 300; count++)
{
lines_read++;
strcpy(stk, "(");
for (int point = lines_read; point < lines_read+10; point++)
{
getline(text, line);
strcat(stk, line.c_str());
strcat(stk, ",");
}
strncpy(stck, stk, 60);
strcat(stck, ")");
strncpy(stack, stck, 61);
message = stack;
cout << "stack = " << stack << endl;
return *message;
}
text.close();
}}]
Last edited on
The design here makes it difficult for the function to ever return more than the first block of lines.

Each time it is called the function would start again from the beginning of the file.

I can think of two possible solutions.

1. Don't try to return anything from the function. Instead, after getting a block of ten lines, call some other function to process that block.

alternatively,

2. Don't open and close the file inside this function. Instead pass by reference the ifstream which has already been opened.

General comments. There are a good many global variables used. I noticed these:
1
2
3
4
5
lines_read
stk
stck
stack
line

That's not good practice. Instead of using global variables, try to make your functions self-contained, and where necessary, use function parameters or function return value to pass information in a controlled fashion between different parts of the program.

Mix of C++ std::string line and various c-strings. I'd recommend using the C++ string throughout.

Thanks for your reply @Chervil.
Unfortunately I still need to use return function...bcoz actually im work with client server operation using winsock. And my client is actually is robot arm. Thus, from this function I read text file and return the value to server code before send it to client. For example, my text file data contain data like this:

0,1,0
1,1,1
2,1,3
.
.
.
.
.
9,9,2

but client can be read with this format (0,1,0,1,1,1,2,1,3........9,1,3).
Bcoz of that, need to send data with correct format to make client can read.
If i cant use return function, i will not able to send data to client..
Well, the second option then sounds like the way to go.

chervil wrote:
2. Don't open and close the file inside this function. Instead pass by reference the ifstream which has already been opened.


Other possibilities. Make the ifstream a global variable (not really ideal). Or make it a static variable within the function so it retains its value from one call to the next.
Last edited on
Im sorry @Chervil..
If u dont mind, can u explain it more detail. What u mean i cant open and close the file inside function. Actually im still beginner in this field.
I would appreciate if u can help me to understand more or maybe can provide me with link to i learn.
Well, I'm still not sure exactly what limitations there are on how your function will be called - for example it currently returns a single char - the first letter of message, and has an input/output parameter char* &message.

Without knowing in more detail how the function will be used, it's difficult to give an appropriate answer.

What doesn't make sense at present is that the function has a loop:
for (int count = 0; count < 300; count++)
but it will always exit after the first iteration.

The only way I can see this making sense if that loop is moved outside the function, or perhaps your client should be controlling that loop.

As for the ifstream, the most important thing is to not have to start from the beginning of the file each time. It needs to continue reading from where it left off on the previous call. Here I'm assuming the function will be called repeatedly.

Here's one idea, but I really don't know how it would fit in your scenario. It uses static variables which retain their value from one call to the next. Also, I tried to use C++ string as far as possible.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

char Mosaic(const char* &message);

int main()
{
    const char *message;        
    
    do {
        Mosaic(message);
        if (message)
            cout << message << '\n';
    } while (message);
}


char Mosaic(const char* &message)
{
    static bool first = true;
    static string data;
    
    static ifstream text;
    if (first)
    {
        first = false;
        text.open("Data.txt");
    }

    if (text)
    {
        data = '(';
        string line;
        int count = 0; 
        while (count < 10 && getline(text, line))
        {
            count++;
            if (count > 1)
                data += ',';
            data += line;
        }
        data += ')';
        message = data.c_str();
    }
    else
        message = NULL;      
        
    return 0;  
}


Wow..amazing..
@Chervil it working...it really work with my system...
thank you @Chervil for help me solve this problem...
Im really appreciate your help...that only part that i stuck...
May God bless you...thanks @Chervil :-)
@joho Though there is something positive there, I'd much prefer you to ask more questions about the things you don't understand. If you are amazed, it sounds like the code is still somehow mysterious. If that's the case, I didn't help you very much at all.
@Chervil sorry for late reply..from your given code..mostly i understand..only part on using static term..i dont really understand bcoz it beyond on my basic knowledge of c++ programming..but that code is work on my system...
Topic archived. No new replies allowed.