-1
int main(int argc, char* argv[]) {
  ifstream ifs(argv[1],ios::in); 

 if(argc!=2)  {
    cout << "Please type main.exe and file name to run the program! Please Try Again" << endl;
}

ifs.open(argv[1]);  

if (!ifs.is_open())  {
    cout << "wrong file name! please open again!" << endl;
}

char line[80]; 
char *point; 

while(ifs.getline(line,80))   //problem seem to be here
{
    cout << "line =" << line << endl;
    point = strtok(line, " ");  
       while(point!=NULL) {
          if (checkdigit(point)) 
               numberofdight++;
        else if(checkkeyword(point)) 
             keywords++;
      else  { }
  }
     cout << point <<endl;
     point = strtok(NULL, " ");
  }
}
ifs.close();

}

The program didn't pass through the while loop while(ifs.getline(line,80)). Can someone help me, please? Pretty new to programming. Suppose to store everything from text file in array. TIA

Lamour
  • 3,002
  • 2
  • 16
  • 28
Vincent Lynn
  • 31
  • 1
  • 6
  • 2
    Better use a `std::vector` and `std::getline()`. Also `strtok()` isn't the best choice for splitting a string. – user0042 Oct 03 '17 at 02:32
  • 1
    Tried to format your code, but your braces are out of whack. – user4581301 Oct 03 '17 at 02:32
  • Indentation is very important to communicating your intent. If you can't be bothered to fix it that reflects badly. – tadman Oct 03 '17 at 02:42
  • Also as part of your introduction to C++ you must come to use `std::string` and stop using C-style character buffers before you hurt someone. Those are extremely risky and will cause endless trouble. Also "array" means using `std::vector` or some other container from the Standard Library. – tadman Oct 03 '17 at 02:43
  • In addition to what @tadman mentioned, https://stackoverflow.com/questions/312570/what-are-some-of-the-drawbacks-to-using-c-style-strings .. Might be good read for you. – spt025 Oct 03 '17 at 03:23
  • @tadman My bad. This is my very first post here and pretty new to programming. I will try to do better in the future. Thanks for your advice. – Vincent Lynn Oct 03 '17 at 03:43
  • Not trying to be overly harsh here, just reminding you that operating C++ is dangerous if you're not paying absolute attention, like handling a chainsaw, whereas C is like handing a greasy chainsaw, slippery at the best of times. An essential guide to understanding C++ is [the book by the language's primary architect](http://www.stroustrup.com/4th.html), though there are others that can build on that and explain things like the Standard Library or common extensions like Boost a lot better. – tadman Oct 03 '17 at 04:06

1 Answers1

0

So the place where you have told that problem seems to be here, I will give an answer around that.

First of I would suggest you read about getline() API to understand how it works, this will help in longer run. (Because you mentioned you are new)

As far as your use of API goes.. while(ifs.getline(line,80))

  1. You can check if file is not empty using ifs.eof() . Reference for this API.
  2. If your line is longer than 80 characters then also it will skip the loop.

So there are these two possibilities which might have been the reason for your control not going inside while, Debug and figure out actual reason. If still not solved, put the file which you are reading.

EDIT : Try out this code with your file. Working code for your reference. (Inspired from your problem statement code)

NOTE: Read about all APIs before using them

#include<iostream>
#include<fstream>

int main(int argc, char* argv[]) {
    ifstream ifs;

    if(argc!=2)  {
        std::cout << "Please type main.exe and file name to run the program! Please Try Again" << std::endl;
    }

    ifs.open(argv[1], std::ifstream::in);

    if (!ifs.is_open())  {
        cout << "wrong file name! please open again!" << endl;
    }

    char line[80];
    char *point; //NOT needed for my samplecode, have just kept from your code

    while(ifs.getline(line,80))   //problem seem to be here
    {
        cout << "line =" << line << endl;
        //TODO:: Write your business logic here.
    }

    ifs.close();
}
spt025
  • 2,134
  • 2
  • 20
  • 26