GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Apr-2005, 04:29
M3X M3X is offline
New Member
 
Join Date: Apr 2005
Posts: 1
M3X is on a distinguished road
Unhappy

not opening file correctly


Hi all!

Let me try to explain what my problem is...

I'm trying to create a program that opens a file containing a word list (words.txt), and then open some logfile(s)...

I'm using command-line arguments to pass the names of the logfiles...

Now for the problem:
When i'm using it from within dos, and passing logfiles as parameters everything works fine...
But for some reason when i drag & drop logfiles over to my app icon, it seems to open words.txt, but it doesn't read anything from it (looking at WX/WY).
It does however, correctly open and read the files dragged into my app...

Does anyone know what is causing this strange behaviour or know a way around this problem??
Thanks in advance!

Heres the code:
BTW i'm using bcc32, i've also tried it in dev-c++ but it made no difference...
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <stdlib>

int main(int argc, char *argv[]){
  char buffer[2048];
  int wx=0, wy=0;

  ifstream wfile("words.txt");
  if(!wfile){cout << "Could not open wordfile." << endl;}
  while(wfile.getline(buffer, 2048)){
    wy++;
    while(buffer[wx]!=0){wx++;}
  }
  wfile.close();

  cout << "MAX Wordlength:" << wx << endl;
  cout << "MAX Words     :" << wy << endl;
  cout << "Number of logfiles:" << argc-1 << endl;

  ifstream logfile[256];
  for(int i=1;i<argc;i++){
    logfile[i-1].open(argv[i]);
    if(!logfile[i-1]){cout << "  Error opening: " << argv[i] << endl;}
    else{
      cout << "  Parsing: " << argv[i] << endl;
      logfile[i-1].getline(buffer, 2048);
      cout << "    " << buffer << endl; //Printing one line
    }
    logfile[i-1].close();
  }

  system("pause");
  return 0;
}
  #2  
Old 16-Apr-2005, 09:27
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by M3X
Hi all!

Let me try to explain what my problem is...

I'm trying to create a program that opens a file containing a word list (words.txt), and then open some logfile(s)...

I'm using command-line arguments to pass the names of the logfiles...

Now for the problem:
When i'm using it from within dos, and passing logfiles as parameters everything works fine...
But for some reason when i drag & drop logfiles over to my app icon, it seems to open words.txt, but it doesn't read anything from it (looking at WX/WY).
It does however, correctly open and read the files dragged into my app...

Does anyone know what is causing this strange behaviour or know a way around this problem??
Thanks in advance!

My initial guess is:
CPP / C++ / C Code:
ifstream wfile("words.txt");
Check your desktop shortcut to make sure the "Start in:" box is filled in with the path of the words.txt file.

Add more output statements to see what's happening.

Also system("pause"); is an improper way to pause your program. Check this tutorial for better options.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 16-Apr-2005, 09:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by M3X
Hi all!

Does anyone know what is causing this strange behaviour or know a way around this problem??

The expression "strange behavior" is somewhat subjective.

I'll comment on the last issue first: It's a command line program; run it from a command line.

As for the rest: Make the program tell you what's happening.

For example, how do you expect a program to get the names of several arguments by dragging and dropping onto its icon? (That is not a rhetorical question; I really don't know the answer.)

You can try something like this and experiment with it. As soon as you drop a file onto the program icon, the program begins execution, right? Then what?

CPP / C++ / C Code:
  cout << "argc = " << argc << endl;
  for (int i = 0; i < argc; i++) {
    cout << "argv[" << i << "] = "  << argv[i] << endl;
  }

Sometimes when I have trouble opening files, I do something like the following to make sure the program is looking in the right directory.

CPP / C++ / C Code:
  char dirname[80];
  getcwd(dirname, 80);
  cout << "directory: " << dirname << endl;
For borland bcc32, #include <dir.h> to get the prototype for getcwd().

If you think the file is being opened correctly but don't know if the program is reading anything, print out each line as it is read (start with a small file with only a few words). Something like this:

CPP / C++ / C Code:
  if(!wfile) {
    cout << "Could not open wordfile." << endl;
  }
  else {
    cout << "Opened words.txt" << endl;
    while(wfile.getline(buffer, 2048)){
      cout << "wy = " << wy << endl;
      cout << "buffer: <" << buffer << ">" << endl;
      wy++;
      while(buffer[wx]!=0){
        wx++;
      }
      cout << "wx = " << wx << endl;
    }
    wfile.close();

    cout << "MAX Wordlength:" << wx << endl;
    cout << "MAX Words     :" << wy << endl;
  }

Get the idea? Once the program is up and running, if you don't see what you expect, make it tell you what's happening.

Regards,

Dave
  #4  
Old 16-Apr-2005, 12:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x
how do you expect a program to get the names of several arguments by dragging and dropping onto its icon? (That is not a rhetorical question; I really don't know the answer.)

Well, for Windows users, that's kind of a simple question with a simple answer.
So, I'll share what I found out in my own simple way.

Using Windows Explorer, I made a desktop shortcut to the executable.
Then, again with Windows Explorer, I selected three text files and dragged-and-dropped them to the desktop icon. File "words.txt" was in the same directory as the executable.

The program opened and read the words.txt file.

The other files showed up as arguments 1, 2, and 3 and the program printed the first line of each.

I have never done this before (drag-and-drop files to an icon for a command line program), since I (almost) always work from a command line. Anyhow, now that I can see it works for me, the previous suggestions that I made should let you see what's happening with yours.

Regards,

Dave

I wish that I could say that, "I learn something new every day," but according to my recent logs, I have been learning something new about every 12.3 days. Unfortunately, my short-term memory seems to have a half-life of about 6.15 days. The odds that it will stick until the next new thing: something worse than 50-50.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:35.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.