Quantcast

Maximum PC

It is currently Mon Dec 29, 2014 2:22 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: C++ file parsing help
PostPosted: Fri Feb 20, 2009 12:02 am 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5550
I'm trying to use the fstream to parse a text file. I've looked at sample code and the compiler (I'm using MS VS 2008) is fine. But I have a feeling it's not even reading the file I'm wanting it to (it's a simple text file).

Code:
#include <iostream>
#include <fstream>
#include "file_parser.h"

using namespace std;

void file_parser::read_file()
{
   int file_length;
   fstream asm_code;

   asm_code.open(file_name.c_str(),ios_base::binary);

   asm_code.seekg(0,ios_base::end);
   file_length = asm_code.tellg();
   asm_code.seekg(0,ios_base::beg);

   buffer = new char[file_length];

   asm_code.read(buffer,file_length);
   asm_code.close();
}


Basically file_length isn't getting the length of the text file, which is what I want. I've made sure that the file name is right and the file itself is in the same directory as the executable (<project>/debug). Or if there's a way of copying the contents of the text file without knowing its length, that'd be fine.

On a personal note, I want to go back to C# :(


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 5:03 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Try something like this...
Code:
StreamReader *reader=new  StreamReader(textFile);
while(reader->Peek() != -1);

KB 307398

Why are you doing it in C++? C# and VB.NET are much better suited to these kinds of tasks, IMO.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 6:45 am 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5550
Thanks, I'll give that a shot. It's for a class. I've gone from using VS2008 C# to programming in UNIX shells. But hey, good experience. I'm developing the code in VS2008 anyways because I don't want to work with g++ though.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 6:59 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Uh... I don't know that stuff developed VS 2k8 stuff will work under unix. What class is it for? An operating systems type class?

You will want to probably avoid VS for this. Try Dev C++ or something maybe?


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 8:18 am 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5550
It's a system's programming class. And it'll work. VS has basically reverted to "you're on your own" mode since Intellisense doesn't work unless I'm calling a method and all the new files start off blank. Plus a few other people are doing it. Unless VS2008's compiler does some different things compared to g++ that are drastic, I don't think it'll be a problem.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 8:53 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Well, good luck to you then...

All I am saying is that .NET is very different from GCC/G++ and if you are targeting a UNIX/Linux platform, it makes sense to use the compiler tools for that platform.

I mean, it isn't like I ever took that class before... :roll:


Top
  Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2009 9:49 am 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5550
The problem still is that it can't read the file. It won't even open it. I've basically tried something simple like:
Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{   
   ifstream code;
   
   code.open("sample.txt", ios::in);

   if (!code)
   {
     cerr << "Can't open input file " << endl;
   }
}


And the if is always triggered. Argh, what I get for pretty much not caring about C++ for two years.

EDIT:
Tried compiling it with g++ with an else statement. It worked.

*sighs* looks like I have to learn to debug with g++ then.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.