No another Text File =]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • steele20
    New Member
    • Sep 2006
    • 6

    No another Text File =]

    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.



    Code:
     #include <iostream> 
    #include <stdlib.h>
    #include <string>
    #include <fstream>
    #include <stdio.h>
    using namespace std;
    void main()
    {
     
    char ch;
    char z;
    char filename[30];
    char fileinfo[100];
    	cout<<"what is the name of the file? "<<endl;
    	cin>> filename;
    FILE *inFile = fopen(filename, "r");
     
    	if (!inFile)
    	{
    		printf("Cannot find the file\n");
    	}
     
     
    	while ((ch = fgetc(inFile)) != EOF)
     
    	------------------
    while ((ch != '\n' || ch != ' ' ) 	//I know this line is bad
    	{
    	for (int i = 1; i < 100; i ++) //this is my touble area
    	 {
    	 fileinfo[i] = ch;
    }
    	}
    	--------------------------
    cout << fileinfo;
     
    	fclose(inFile);
    	cout << endl;
    cin >> z; //here so command prompt doesn't close
    }
    Last edited by Niheel; Sep 1 '06, 05:16 PM.
  • zeny
    New Member
    • Jul 2006
    • 44

    #2
    I think it would be much more easy to read the whole line at a time and then tokenize it.

    Comment

    • steele20
      New Member
      • Sep 2006
      • 6

      #3
      hmm, not sure how to tokenize it though. I'm a bit dumb =\

      Comment

      • steele20
        New Member
        • Sep 2006
        • 6

        #4
        also if I use

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


        Doesn't this apply only for the first line-

        10 20 3

        Comment

        • rgb
          New Member
          • Aug 2006
          • 37

          #5
          tokenize sample link: http://www.cplusplus.com/ref/cstring/strtok.html

          Comment

          Working...