Read in Words to Vector From File C++
-
01-eighteen-2011 #i
C++ Beginner !!!
Reading a text file to a vector
Hi!
I wanted to use vectors to read my input text file.
I dont know what is going wrong in the code.
Can anyone comment?
thanks
Lawmaking:
#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 = southward + ane; } vector<int>myints1(s); vector<int>myints2(south); for (i = 0; i <s;i++){ scanf("%d",&myints1[i]); printf("%d",myints1[i]); } return 0; }
-
01-18-2011 #2
and the hat of int overfl
Well the first problem is, you're using C I/O in a C++ program.
Driblet stdio.h and utilise <fstream> and <iostream>2d, you count characters, and then yous try to read integers (which presumably occupy more than than one graphic symbol). Counting is a fleck of a waste of effort, since vectors tin be resized on the wing a lot faster than reading the file twice. Look for vector.push_back()
-
01-xviii-2011 #three
C++ Beginner !!!
I am able to read my file using the vectors equally
But I still accept 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) != NULL) { if (sscanf(buffer, "%d %d", &val1, &val2) != 2) { printf("Mistake\north"); } 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 2 columns as
ane four
two iii
three ii
4 fiveI want to go through the first column ans then the second, if there is a value that is present int the second column but not in first I desire to get that (in this case value 5)
and want to swap the values.I tried with for loop but dint work for me.
Whatever suggestions???
-
01-18-2011 #iv
and the Chapeau of Guessing
Desire to swap ... which values?
-
01-18-2011 #five
C++ Beginner !!!
in this particular case, if 5 is not in the showtime column then brand it the start entry and the associated value the secon
the the file will look similar
1 four
2 three
3 2
4 5i 4
2 3
three ii
5 4
-
01-18-2011 #six
and the hat of int overfl
Then what is difficult with doing
Or whatsoever the examination is that decides how y'all bandy the 2 elements.Code:
if ( v1[i] < v2[i] )
-
01-18-2011 #7
Registered User
FWIW, merely to point out something with the first lawmaking sample y'all posted:
fgetc returns an int, not a char. This is an of import item.Lawmaking:
char c; fp = fopen("10_1.txt","r"); // reading the file while((c=fgetc(fp))!=EOF)
"Owners of dogs will have noticed that, if y'all provide them with food and water and shelter and affection, they will recollect you are god. Whereas owners of cats are compelled to realize that, if y'all provide them with food and water and shelter and amore, they draw the conclusion that they are gods."
-Christopher Hitchens
-
01-19-2011 #viii
C++ Beginner !!!
thanks....
I worked information technology out.But I have a part that gives me the intersection of two vectors. But I dont know the return types of this function.
I want to laissez passer the value returned by the Intersection function to another function merely get an error every bit
fault: invalid conversion from 'int (*)()' to 'int'The Intersection function is
It returns 3 values as one, 5, seven.Code:
int Intersection() { fix<int>::iterator it; ready<int>::iterator iterr; sort (v1.begin(), v1.brainstorm()+v1.size()); sort (v2.brainstorm(), v2.begin()+v2.size()); set_intersection (v1.brainstorm(), v1.begin()+v1.size(),v2.begin(), v2.brainstorm()+v2.size(), v.begin()); set up<int> vset(v.brainstorm(), v.end()); set<int>newset(v1.begin(),v1.end()); intersection = int(vset.size()); printf("the intersection cardinality is %d\n", intersection); for (iterr=newset.brainstorm(); iterr!=newset.end(); ++iterr){ bool flag = false; for (it=vset.begin(); it!=vset.end(); ++it) { if (*iterr == *information technology) { flag = true; } } if (!flag) printf("\nThe bodily values are %d \n", *iterr); } printf("\northward"); render *iterr; }I want to pass these values into another function. say Catechumen defined as
void Convert(int position)
but not able to
-
01-19-2011 #9
and the Hat of Guessing
If you want to return a ready, then return a set.
As to the fault, by the time you reach the end of your function, iterr == newset.end(), meaning it'southward non a valid iterator, significant trying to dereference it gives bad things.
Source: https://cboard.cprogramming.com/cplusplus-programming/133878-reading-text-file-vector.html
0 Response to "Read in Words to Vector From File C++"
Post a Comment