Read in Words to Vector From File C++

  1. #i

    satty is offline

    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; }


  2. #2

    Salem is offline

    and the hat of int overfl Salem's Avatar


    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()


  3. #three

    satty is offline

    C++ Beginner !!!


    Thumbs up

    I am able to read my file using the vectors equally

    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]);     }
    But I still accept issues of reading the columns and comparing them.
    My file has 2 columns as
    ane four
    two iii
    three ii
    4 five

    I 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???


  4. #iv

    tabstop is offline

    and the Chapeau of Guessing tabstop's Avatar


    Desire to swap ... which values?


  5. #five

    satty is offline

    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 5

    i 4
    2 3
    three ii
    5 4


  6. #six

    Salem is offline

    and the hat of int overfl Salem's Avatar


    Then what is difficult with doing

    Code:

    if ( v1[i] < v2[i] )
    Or whatsoever the examination is that decides how y'all bandy the 2 elements.


  7. #7

    hk_mp5kpdw is offline

    Registered User hk_mp5kpdw's Avatar


    FWIW, merely to point out something with the first lawmaking sample y'all posted:

    Lawmaking:

                            char  c;                        fp = fopen("10_1.txt","r");    // reading the file while((c=fgetc(fp))!=EOF)
    fgetc returns an int, not a char. This is an of import item.

    "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


  8. #viii

    satty is offline

    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

    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;    }
    It returns 3 values as one, 5, seven.

    I want to pass these values into another function. say Catechumen defined as
    void Convert(int position)
    but not able to


  9. #9

    tabstop is offline

    and the Hat of Guessing tabstop's Avatar


    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.


preissthiped.blogspot.com

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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel