423,674 Members | 1,946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,674 IT Pros & Developers. It's quick & easy.

I've tried this from all angles and its proving to be difficult

P: 3
//reads data from the ICSA csv file
if (!inputICSA.is_open())cout<<"ERROR: FILE NOT LOCATED\n";

string a_admission, a_name, a_fname, a_gender;

cout << "\n\tICS A\n\n";

while (inputICSA.good())
{
getline(inputICSA, a_admission, ',');
getline(inputICSA, a_name, ',');
getline(inputICSA, a_fname, ',');
getline(inputICSA, a_gender);

inputICSA.get();

cout << "Admission No: " << a_admission << "\t" << "Name: " << a_name << a_fname << "\t" << "gender: " << a_gender << endl;

}
I'm required to import three csv files and then sort them to make two new csv files from the shuffle. I've only been able to import the three csv files and i've been stuck at that. I'm a newbie hence the difficulties.
Jul 4 '18 #1

✓ answered by dev7060

Here's a pseudo code/algo I have come up with:

You need five file pointer variables.

Let's say: var1, var2 and var3: for reading from the existing files (ios::in mode)

var4 and var5: for writing to the new files (ios:: in | ios::out mode , since both reading and writing are to be done with the new files).

The basic idea is to read from the existing files and place that data accordingly to the new files using some 'if' conditions.


Expand|Select|Wrap|Line Numbers
  1. while (var1.good()){    
  2.   READ a_admission, a_name, a_fname, a_gender from the file using var1
  3.  
  4.   if(a_gender=='1'){
  5.        //It is now clear (with the help of a_gender)that the data belongs to the first newly created file
  6.        //Therefore, pointer dealing with the first split is to be used i.e. var4 for writing
  7.        //Since the data should be written in the sorted form (assuming the sorting 
  8.          takes place as per the names), the place of where to put that data is to be 
  9.          decided
  10.        -CODE to determine the proper location
  11.        -Insertion of the data using 'var4'
  12.   }
  13.   else if(a_gender=='2'){
  14.        //using var5 to write to the second created file
  15.        -CODE to determine the proper location
  16.        -Insertion of the data using 'var5'
  17.   }
  18. }
  19. while (var2.good()){
  20.      -READING with var2
  21.      -same as above-
  22. }
  23. while (var3.good()){
  24.      -READING with var3
  25.      -same as above-
  26. }
You may also require the functions like seekp, seekg etc. for the proper positioning of the file pointer before reading/writing.

Share this Question
Share on Google+
5 Replies


weaknessforcats
Expert Mod 5K+
P: 9,186
What do you mean by sort?

What are the two new CSV files? Are you splitting by gender?


Need more info here. I don't see where a sort fits in.
Jul 4 '18 #2

P: 3
I'm supposed to create two new csv files after the shuffle. Yes, i'm supposed to split by gender.
Jul 5 '18 #3

P: 27
Here's a pseudo code/algo I have come up with:

You need five file pointer variables.

Let's say: var1, var2 and var3: for reading from the existing files (ios::in mode)

var4 and var5: for writing to the new files (ios:: in | ios::out mode , since both reading and writing are to be done with the new files).

The basic idea is to read from the existing files and place that data accordingly to the new files using some 'if' conditions.


Expand|Select|Wrap|Line Numbers
  1. while (var1.good()){    
  2.   READ a_admission, a_name, a_fname, a_gender from the file using var1
  3.  
  4.   if(a_gender=='1'){
  5.        //It is now clear (with the help of a_gender)that the data belongs to the first newly created file
  6.        //Therefore, pointer dealing with the first split is to be used i.e. var4 for writing
  7.        //Since the data should be written in the sorted form (assuming the sorting 
  8.          takes place as per the names), the place of where to put that data is to be 
  9.          decided
  10.        -CODE to determine the proper location
  11.        -Insertion of the data using 'var4'
  12.   }
  13.   else if(a_gender=='2'){
  14.        //using var5 to write to the second created file
  15.        -CODE to determine the proper location
  16.        -Insertion of the data using 'var5'
  17.   }
  18. }
  19. while (var2.good()){
  20.      -READING with var2
  21.      -same as above-
  22. }
  23. while (var3.good()){
  24.      -READING with var3
  25.      -same as above-
  26. }
You may also require the functions like seekp, seekg etc. for the proper positioning of the file pointer before reading/writing.
Jul 5 '18 #4

P: 3
Thanks alot for your input. When you say code to determine the proper location, which algorithm is the best to use?
Jul 6 '18 #5

P: 27
Not sure about any best algo. You can google things for help and implement the one which suits you the best.
Jul 6 '18 #6

Post your reply

Sign in to post your reply or Sign up for a free account.