Reading a text file to a vector

This is a discussion on Reading a text file to a vector within the C++ Programming forums, part of the General Programming Boards category; Hello! I wanted to use vectors to read my input text file. I dont know what is going wrong in ...

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Reading a text file to a vector

    Hello!
    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
    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 + 1;
        }
      
      vector<int>myints1(s);
      vector<int>myints2(s);
      for (i = 0; i <s;i++){
        scanf("%d",&myints1[i]);
        printf("%d",myints1[i]);
      }
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well the first problem is, you're using C I/O in a C++ program.
    Drop stdio.h and use <fstream> and <iostream>

    Second, you count characters, and then you 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. Look for vector.push_back()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    Thumbs up

    I am able to read my file using the vectors as
    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("Error\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]);
        }
    But I still have issues of reading the columns and comparing them.
    My file has 2 columns as
    1 4
    2 3
    3 2
    4 5


    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 want to get that (in this case value 5)
    and want to swap the values.

    I tried with for loop but dint work for me.
    Any suggestions???

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Want to swap ... which values?

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    in this particular case, if 5 is not in the first column then make it the first entry and the associated value the secon

    the the file will look like
    1 4
    2 3
    3 2
    4 5


    1 4
    2 3
    3 2
    5 4

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    So what is hard with doing
    Code:
    if ( v1[i] < v2[i] )
    Or whatever the test is that decides how you swap the two elements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    FWIW, just to point out something with the first code sample you 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 food and water and shelter and affection, they will think 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 draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanks....
    I worked it out.

    But I have a function that gives me the intersection of 2 vectors. But I dont know the return types of this function.
    I want to pass the value returned by the Intersection function to another function but get an error as
    error: invalid conversion from 'int (*)()' to 'int'

    The Intersection function is

    Code:
    int Intersection()
    {
      
      set<int>::iterator it;
      set<int>::iterator iterr;
      sort (v1.begin(), v1.begin()+v1.size());  
      sort (v2.begin(), v2.begin()+v2.size());  
      set_intersection (v1.begin(), v1.begin()+v1.size(),v2.begin(), v2.begin()+v2.size(), v.begin());
      set<int> vset(v.begin(), v.end());
      set<int>newset(v1.begin(),v1.end());
      intersection = int(vset.size());
     
      printf("the intersection cardinality is %d\n", intersection);
      for (iterr=newset.begin(); iterr!=newset.end(); ++iterr){
        bool flag = false;
        for (it=vset.begin(); it!=vset.end(); ++it)
          {   
    	if (*iterr == *it)
    	  {
    	    flag = true;    
    	  }
          }
        if (!flag)
          printf("\nThe actual values are %d \n", *iterr);
      }
      printf("\n");
      return *iterr;
      
    }
    It returns 3 values as 1, 5, 7.

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

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to return a set, then return a set.

    As to the error, by the time you reach the end of your function, iterr == newset.end(), meaning it's not a valid iterator, meaning trying to dereference it gives bad things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Reading output into a text file
    By pete212 in forum C Programming
    Replies: 8
    Last Post: 04-23-2008, 05:11 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21