Create a c++ program that reads a CSV file and prints the rows at even positions. For example, if the CSV file has 10 rows, print rows 2, 4,6, 8 and 10

Recommended Answers

All 5 Replies

This seems like a homework assignment. What do you have so far? Where are you stuck?

commented: I applied for RPA developer job they given 3 tasks to do I don't know much about C++ I checked google but I am stuck . +0

In a weird way, C++ makes this harder than plain C, provided that you know the maximum line length in advance. (The strtok() function from <string.h> will help split up a C string into comma-separated tokens.) That idea probably won't get a good grade on your C++ assignment, even though it's technically legal & standard to include <cstring> and use the C-style string library.

What I'd suggest is to use std::getline() (the function from <string>, not the getline method of std::cin) to read whole lines into C++ string object, then use the string's .find() member function. In a string named "line", line.find(',') will return the index of the first comma and line.find(',', cpos+1) will find the index of the next comma after some previous comma position "cpos".

Use the .substr() member function to extract the individual values. There are a bunch of options, but the most common use you'll have is to use line.substr(start, stop-start) to extract characters starting at line[start] up to but not including line[stop]. In your program, (start) will be the index of the first character of a comma-separated value, and (stop) will be the index of the comma that end the value.

See docs at: http://www.cplusplus.com/reference/string/string/
or more authoritative (but a bit less readable) docs at: https://en.cppreference.com/w/cpp/string/basic_string

Maybe this is easier than it sounds. The CSV lines don't split over a CR/LF (see https://en.wikipedia.org/wiki/Comma-separated_values ) so for this assignment there appears to be no need to parse the CSV content.

Just a simple while loop with a conditional if and print statements.

Psuedo code might be:

open input file.
while not eof input file
    {
    readline()
    if not eof input file
        {
            readline()
            print a line
        }
    }
close input file
commented: I don't know how to print the rows in even manner +0

A, consider the following.

  1. "Create a c++ program that reads a CSV file and prints the rows at even positions. For example, if the CSV file has 10 rows, print rows 2, 4,6, 8 and 10"
  2. "print the rows in even manner"

Sorry but I can't begin to guess what "print the rows in even manner" means here. Your top post actually doesn't sound hard at all but maybe there's more to the assignment than stated?

commented: I don't know how to print the rows in even manner +0

Please explain what is " print the rows in even manner ".

The top post appears to ask to "print the even rows" and now you ask " print the rows in even manner " so you'll have to explain in detail how this differs.

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.