Read Input From Text File and Sort Into Vector C++
-
01-18-2011 #i
C++ Beginner !!!
Reading a text file to a vector
Hello!
I wanted to utilise vectors to read my input text file.
I dont know what is going incorrect in the code.
Can anyone annotate?
thanks
Code:
#include <stdio.h> #include <stdlib.h> #include <algorithm> #include <vector> using namespace std; int main() { FILE *fp; int s = 0; char c; fp = fopen("10_1.txt","r"); // reading the file while((c=fgetc(fp))!=EOF) { if(c=='\n') s = s + one; } vector<int>myints1(due south); vector<int>myints2(s); for (i = 0; i <s;i++){ scanf("%d",&myints1[i]); printf("%d",myints1[i]); } render 0; }
-
01-18-2011 #two
and the hat of int overfl
Well the first problem is, you're using C I/O in a C++ plan.
Drop stdio.h and utilize <fstream> and <iostream>Second, you count characters, and then you lot try to read integers (which presumably occupy more than one character). Counting is a bit of a waste of effort, since vectors can be resized on the fly a lot faster than reading the file twice. Await for vector.push_back()
-
01-18-2011 #3
C++ Beginner !!!
I am able to read my file using the vectors every bit
Merely I still take issues of reading the columns and comparing them.Code:
vector<int> v1; vector<int> v2; vector<int> v(30); vector<int>::iterator it; fp = fopen("10_1.txt","r"); while(fgets(buffer, 49, fp) != Zero) { if (sscanf(buffer, "%d %d", &val1, &val2) != 2) { printf("Fault\n"); } else { v1.push_back(val1); v2.push_back(val2); } } for(unsigned int i=0;i<v1.size();i++) { printf("%d %d\n", v1[i], v2[i]); }
My file has ii columns every bit
i 4
two 3
3 two
four 5I want to get through the first column ans then the 2d, if there is a value that is present int the 2nd column but non in starting time I want to get that (in this example value 5)
and desire to swap the values.I tried with for loop only dint piece of work for me.
Any suggestions???
-
01-18-2011 #4
and the Lid of Guessing
Desire to bandy ... which values?
-
01-xviii-2011 #5
C++ Beginner !!!
in this particular instance, if five is not in the first cavalcade so go far the first entry and the associated value the secon
the the file will look similar
1 4
two 3
3 ii
4 51 4
2 3
3 2
5 4
-
01-eighteen-2011 #6
and the hat of int overfl
So what is hard with doing
Or whatever the test is that decides how y'all swap the two elements.Lawmaking:
if ( v1[i] < v2[i] )
-
01-18-2011 #vii
Registered User
FWIW, just to point out something with the offset code sample you lot posted:
fgetc returns an int, not a char. This is an important detail.Code:
char c; fp = fopen("10_1.txt","r"); // reading the file while((c=fgetc(fp))!=EOF)
"Owners of dogs will have noticed that, if you provide them with nutrient and water and shelter and affection, they volition recollect you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they describe the determination that they are gods."
-Christopher Hitchens
-
01-19-2011 #8
C++ Beginner !!!
thanks....
I worked information technology out.But I have a function that gives me the intersection of ii vectors. But I dont know the render types of this function.
I want to laissez passer the value returned past the Intersection function to another function but get an mistake as
mistake: invalid conversion from 'int (*)()' to 'int'The Intersection function is
It returns 3 values as one, 5, vii.Lawmaking:
int Intersection() { set<int>::iterator it; prepare<int>::iterator iterr; sort (v1.begin(), v1.begin()+v1.size()); sort (v2.begin(), v2.begin()+v2.size()); set_intersection (v1.begin(), v1.brainstorm()+v1.size(),v2.begin(), v2.begin()+v2.size(), v.begin()); ready<int> vset(5.begin(), v.cease()); set<int>newset(v1.begin(),v1.end()); intersection = int(vset.size()); printf("the intersection cardinality is %d\due north", intersection); for (iterr=newset.brainstorm(); iterr!=newset.end(); ++iterr){ bool flag = imitation; for (it=vset.begin(); it!=vset.finish(); ++it) { if (*iterr == *it) { flag = true; } } if (!flag) printf("\nThe actual values are %d \n", *iterr); } printf("\northward"); render *iterr; }I want to pass these values into another function. say Catechumen divers as
void Convert(int position)
but not able to
-
01-nineteen-2011 #nine
and the Hat of Guessing
If yous desire to return a set up, so return a ready.
As to the error, past the time you reach the terminate of your function, iterr == newset.end(), meaning it's not a valid iterator, meaning trying to dereference it gives bad things.
kearneyhibive1970.blogspot.com
Source: https://cboard.cprogramming.com/cplusplus-programming/133878-reading-text-file-vector.html
0 Response to "Read Input From Text File and Sort Into Vector C++"
Post a Comment