FILE I/O

ok so i want to read / write numbers from a file.txt .
how do i tell the machine to do particular things if i come to the end of a line...or at the beginning of a line ?
i dont mean EOF..end of file.. but i will like to do particular things at the beginning of the line and at the end of every row or line..
thanks for your answers in advance :)
Write your question here.

ok so i want to read / write numbers from a file.txt .
This is a very broad question. Do you use C or C++ IO facilities? How do you read something? Do you use formatted input? And so on. Code example would be good here.

One of the solutions in your case would be to read file line by line:
1
2
3
4
5
6
std::ifstream inp("somefile");
std::string temp;
while(std::getline(inp, temp)) {
    parse_string(temp);
    do_something_at_the_end_of_the_line();
}
c++ or better still c++ 11 ...do i really need to give a code?

assume you having a text file containg some random numbers like this
21 34 32
22 12 23
34 34 34

just like how you see these numbers...
after reading the first line and arriving at 32...i will like to stop and do something..
then on before reading 22 i will like to do something...
at after reading 23 last number...i will like to be able to do... :: if 23 is last number on line...do something..then... if new line...do something before starting reading...

i don't thing is that broad...
Example of code:
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int sqr(int x)
{ return x*x; }

void parse_line(const std::string& s, std::ostream& out)
{
    std::istringstream in(s);
    int i;
    while(in >> i) out << sqr(i) << ' ';
}

int main()
{
    std::ifstream input("input.txt");
    std::ofstream output("output.txt");
    std::string temp;
    bool firstline = true;
    while(std::getline(input, temp)) {
        if(not firstline) output << "separating line\n";
        firstline = false;
        parse_line(temp, output);
        output << '\n';
    }
}
thanks MiiNiPaa..but why function sqr? why are you squaring the value taken in?
why are you squaring the value taken in?
Because I want to square them and not simply copy from input to output.
hahah ok..i thougt we were doing my code and not yours :P :P :P ...thanks man anyway :)
ok i got it..you were squaring to give me an example of "do something" ok ok lolz i didn't get it earliar before ;) thanks loads
Topic archived. No new replies allowed.