CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Problem Reading File in C++

    Hi, I'm having trouble opening my file in visual studio. The fexists function returns true, but when gets to the loading in the file it gives me the following error: invalid NULL pointer. When debugging, it says error reading characters from string. The file in notepad looks like this: "1#2#3#4". Can anyone see a reason why this is occurring? Thanks!

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include<string>
    using namespace std;
    
    
    bool fexists(string filename)
    {
      ifstream ifile(filename);
      return ifile;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	string location = "C:\\Users\\Brittany\\Documents\\Visual Studio 2012\\Projects\\ConsoleApplication9\\ConsoleApplication9\\Debug\\test1.txt";
    	bool file_exists = fexists(location);
    	ifstream my_file (location);
    	int poundpos[12];
    	string mystring;
    	
    \	int i = 0;
    	string temp = 0;
    	int add = 0;
    	int temp1 = 0;
    	
    	int length = 20;
    	while(getline(my_file, mystring))
    	 {
    		 //length = stoi(mystring.length);
    		 istringstream liness(mystring);
    		 for(i = 0; i < length; i++)
    		 {
    	       getline(liness, temp, '#');
    		   temp1 = stoi(temp);
    		   temp1 = poundpos[i];
    		   add = poundpos[i] + add;
    		
    		 }
    	 }
    
    	
    	
    	cout << (add);
    	
    	my_file.close();
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem Reading File in C++

    Yes. In fexists, What type is ifile? what type are you returning from fexists? Where in your main program are you testing fexists return value?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Problem Reading File in C++

    Quote Originally Posted by elephant1 View Post
    ... When debugging, it says error reading characters from string. The file in notepad looks like this: "1#2#3#4".
    In what line of your code do you have this error?
    Victor Nijegorodov

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Problem Reading File in C++

    Could you explain what you are btrying to do ?

    There are a number of problems:

    Code:
    int poundpos[12];
    
    // ...
    
    int length = 20;
    
    // ..
    
    for(i = 0; i < length; i++)
    {
       getline(liness, temp, '#');
       temp1 = stoi(temp);
       temp1 = poundpos[i];          // 1. what happens if i>11 ?
                                     // 2. where does poundpos[i] ever get set ?
                                     // 3. you set temp1 ... but overwrite it right away
       add = poundpos[i] + add;    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured