Thread: How read it by C++

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    How read it by C++

    Hello all:

    I used to use C to do the scientific calculation. When I need to read a data file "datafile.txt" into an array "data[]", the following is available:
    Code:
                  FILE *fp;
                  if((fp=fopen("0date6y.txt", "r"))==NULL) 
                    { printf("can not open file date\n");
                      return;
                     }
                  for(i=0; i<=M1; i++) fscanf(fp,"%s", &dt);  //M1: elements of data
    Use the script above, the data can be only one column.

    If the data file has three columns, can I use C++ script to read the 2nd or 3rd column?

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Huh? There is no limit on the number of columns you can have with the code you have posted. Have you tried?

    But if you want to do the same thing in C++, you can do:
    Code:
                  ifstream fp;
                  if (!fp.open("0date6y.txt", ios::in))
                    { cout << "can not open file date\n";
                      return;
                     }
                  for(i=0; i<=M1; i++) cin >>  dt;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read Causes Freezing?
    By Masna in forum C Programming
    Replies: 5
    Last Post: 07-18-2008, 04:31 PM
  2. Read() problems
    By yay4rei in forum C Programming
    Replies: 2
    Last Post: 07-21-2005, 10:47 AM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM