Dear All,

I'm writing a code which could read the following file:
(input.raw) contained:
11 12 13
21 22 23
31 32 33

While reading this file I want to do the following:
-Move cursor to the beginning of that file which is before "11", then start reading
-After reading 11 the ' ' will read in too, in this step I want to skip reading this line by moving the cursor to the beginning of next line which is before "21", then again skip reading this line when ' ' is found, move cursor to the beginning of next line which is before '31'.

Please help me I'm getting stuck in here.

Thanks in advance.

Dani AI

Generated

This task is usually solved by treating the file line-by-line and handling each line independently so the stream cursor moves to the next line after you process the first token. pointed in that direction with a C++ stream/stringstream approach, and suggested discarding the rest of the current line when you only need the first value. Below is a small, portable C-style example that does the same thing without using stringstream (useful if you want simple, predictable behavior and explicit CR/LF handling).

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>

int main(void) {
    FILE *f = fopen("input.raw", "r");
    if (!f) return 1;
    char buf[512];
    while (fgets(buf, sizeof buf, f)) {
        size_t len = strlen(buf);
        while (len && (buf[len-1] == '\n' || buf[len-1] == '\r')) buf[--len] = '\0';

        char *p = buf;
        while (*p && isspace((unsigned char)*p)) ++p;
        if (!*p) continue;                // blank line

        char *end;
        long v = strtol(p, &end, 10);
        if (p == end) continue;          // no integer at start

        // process the first integer (v). The rest of the line is ignored.
        printf("%ld\n", v);
    }
    fclose(f);
    return 0;
}

Notes and troubleshooting

  • This handles CRLF (trims '\r'), ignores blank lines, and parses only the first integer on each line.
  • If you expect arbitrarily long lines, increase the buffer or switch to std::getline/std::string. 's stringstream approach is convenient for robust C++ parsing and token handling.
  • If strict numeric range checks are required, test errno after strtol for ERANGE or use std::stol with try/catch.
  • If you want to keep the first token and then explicitly discard the rest from a stream-based loop, use the stream-version of "read first token, then discard to newline" as suggested.

This gives a clear, deterministic way to read the first value on each line and advance to the next line immediately after.

Recommended Answers

All 2 Replies

After reading the number you need to flush the remainder of the line. Read this Read Me thread to see how to do that.

Use a stringstream to get stuff out of a string.

#include <fstream>
#include <sstream>
#include <string>
...

ifstream inf( "fooey.txt" );
string line;
while (getline( inf, line ))
  {
  // if the line is blank, ignore it
  if (line.find_first_not_of( " \t" ) == line.end()) continue;
  // get the first integer off the line
  stringstream linestream( line );
  int n;
  if (!(linestream >> n))
    that_was_not_an_integer();
  else 
    dosomething( n );
  }

This gets one line at a time from the file, and endeavors to get the first item on the line (if any) as an integer number.

Hope this helps.

[edit] hmm... I'm slow...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.