By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,636 Members | 1,915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,636 IT Pros & Developers. It's quick & easy.

No another Text File =]

P: 6
I have a problem, Im reading numbers from a text file like this

10 20 3

x1 y1

x2 y2

and so on, x and y are values and theres no limit but will probably not be more than 5 or so.

What I want to do is fill an array with 10 at the first spot, 20 at the second spot, 3 at the third spot, and so on and disgregard the white spaces and new lines since im reading one character at a time. (not sure of a better way to do this)

I can open the file and get the characters from it and thats it.



Expand|Select|Wrap|Line Numbers
  1.  #include <iostream> 
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <fstream>
  5. #include <stdio.h>
  6. using namespace std;
  7. void main()
  8. {
  9.  
  10. char ch;
  11. char z;
  12. char filename[30];
  13. char fileinfo[100];
  14.     cout<<"what is the name of the file? "<<endl;
  15.     cin>> filename;
  16. FILE *inFile = fopen(filename, "r");
  17.  
  18.     if (!inFile)
  19.     {
  20.         printf("Cannot find the file\n");
  21.     }
  22.  
  23.  
  24.     while ((ch = fgetc(inFile)) != EOF)
  25.  
  26.     ------------------
  27. while ((ch != '\n' || ch != ' ' )     //I know this line is bad
  28.     {
  29.     for (int i = 1; i < 100; i ++) //this is my touble area
  30.      {
  31.      fileinfo[i] = ch;
  32. }
  33.     }
  34.     --------------------------
  35. cout << fileinfo;
  36.  
  37.     fclose(inFile);
  38.     cout << endl;
  39. cin >> z; //here so command prompt doesn't close
  40. }
  41.  
Sep 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: 44
I think it would be much more easy to read the whole line at a time and then tokenize it.
Sep 1 '06 #2

P: 6
hmm, not sure how to tokenize it though. I'm a bit dumb =\
Sep 1 '06 #3

P: 6
also if I use

char line[100];
fgets(line, 100, inFile);


Doesn't this apply only for the first line-

10 20 3
Sep 1 '06 #4

P: 37
rgb
tokenize sample link: http://www.cplusplus.com/ref/cstring/strtok.html
Sep 1 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.