Read Input From Text File and Sort Into Vector C++

  1. #i

    satty is offline

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


  2. #two

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


  3. #3

    satty is offline

    C++ Beginner !!!


    Thumbs up

    I am able to read my file using the vectors every bit

    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]);     }
    Merely I still take issues of reading the columns and comparing them.
    My file has ii columns every bit
    i 4
    two 3
    3 two
    four 5

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


  4. #4

    tabstop is offline

    and the Lid of Guessing tabstop's Avatar


    Desire to bandy ... which values?


  5. #5

    satty is offline

    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 5

    1 4
    2 3
    3 2
    5 4


  6. #6

    Salem is offline

    and the hat of int overfl Salem's Avatar


    So what is hard with doing

    Lawmaking:

    if ( v1[i] < v2[i] )
    Or whatever the test is that decides how y'all swap the two elements.


  7. #vii

    hk_mp5kpdw is offline

    Registered User hk_mp5kpdw's Avatar


    FWIW, just to point out something with the offset code sample you lot posted:

    Code:

                            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 important detail.

    "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


  8. #8

    satty is offline

    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

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

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


  9. #nine

    tabstop is offline

    and the Hat of Guessing tabstop's Avatar


    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

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel