Loop issues when counting lines in a .csv
It's me again still working on learning how it is possible for me to manage and manipulate .csv files using c++. I've made a small program that creates a new .csv file, fills in the table headings with generic labels, all named "value" for demonstrative purposes. I then close the file to be sure any changes I made are saved. I then re-open that same file and count the number of lines in the file. I again close the file, and now open a file with references to the values that need to be filled in to the newly created file. I then count the lines of this file and compare them to the number of lines of the newly created file. If the new file has fewer lines than the reference file I need to identify the last line of the new file, add one to that number, and then read the values from that number line in the refrence file to then add the values to that new file.
Now comes to my problems. The program will accurately tell how many lines each file has. Great so far, but when i try to say check if the new file has fewer lines on it in a while loop it is doing something to just keep adding to the numbers of line and in an if staement it only checks once. This is probably a really stupid error like, I should have the whole program in a while loop or something but I do not want the first step of in开发者_如何转开发serting the table headings to be repeated. Any help is appreciated.
I may not be wording myself correctly, If anyone is confused by what I;m saying but think that they can help me solve this issue just ask me to clarify.
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
char c;
int x=0;
int y=0;
int loop=1;
ofstream mynewfile;
mynewfile.open("MyNewFile.csv");
if (mynewfile.good())
{
mynewfile << "Value,Value,Value,Value,Value,Value," <<endl;
//End of if mynewfile.good()
}
mynewfile.close();
while(loop==1)
{
ifstream mynewfile2;
mynewfile2.open ("MyNewFile.csv");
while (mynewfile2.good())
{
c = mynewfile2.get();
if (c=='\n')
x++;
//End of while(mynewfile2.good())
}
mynewfile2.close();
ifstream myoldfile;
myoldfile.open ("MyOldFile.csv");
while (myoldfile.good())
{
c = myoldfile.get();
if (c=='\n')
y++;
}
myoldfile.close();
cout << "The Number of lines in the file are " << x << endl;
cout << "The Number of lines in the file are " << y << endl;
if(x < y)
{
cout << "Keep Going" << endl;
}
else if(x = y)
{
system("PAUSE");
}
else
{
loop == 0;
}
}
system("PAUSE");
return 0;
//End of main()
}
You made a classic C/C++ error. This line:
else if(x = y)
assigns the value of y to x. It evaluates to true if y is non-zero. What you meant was this:
else if(x == y)
精彩评论