Thread: loop doesn't read files

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

    loop doesn't read files

    Hello all: My work need to read files into arrays. In the past, only
    several files,I set a file pointer and used fscanf with following codes
    (supose each file only has 3-number in three lines):
    Code:
              if((fp=fopen("file1.txt", "r"))==NULL) 
              {cout<<"can not open file 1"<<endl;);
               return;
              }
              for(int i=0; i<3; i++) fscanf(fp,"%f", &arr[0][i]); //read file1    
              
              if((fp=fopen("file2.txt", "r"))==NULL) 
              {cout<<"can not open file 2"<<endl;);
              return;
              }
              for(i=0; i<3; i++) fscanf(fp,"%f", &arr[1][i]); //read file2
    When running the above codes, the files are in the same folder of cpp
    source codes. The above codes work well except reading files one by one.

    Now the number of files become more. I tried to use loop to avoid one
    by one. But failed! The following is the codes.
    Code:
             char textfile[3][10]={"file1.txt","file2.txt","file3.txt"};
             for(j=0; j<3; j++)
    	 {
               if((fp=fopen("textfile[j]", "r"))==NULL) 
    	   {cout<<"can not open file "<<j<<endl;
                return;
    	   }
             for(i=0; i<3; i++) fscanf(fp,"%f", &arr[j][i]); 	   
            }
    This codes also can pass compiling and linking, but the result
    of excuting is allways: "can not open file[0]"!

    Clearly, "file1.txt" can not be changed to "textfile[0]", although
    textfile[0]=file1.txt;"file2.txt" can not be changed to "textfile[1]",
    although textfile[1]=file2.txt;..... Why?

    Who can help me with the problem? Or suggest me other good way for
    reading files.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    This is more C than C++. You should be using fstreams, and I'm by no means any sort of pro in C File I/O, but isn't there a function you should be calling in each loop. Anyway, forget about that, you use iostreams, you should be using fstreams.
    Last edited by SlyMaelstrom; 05-14-2006 at 08:08 PM.
    Sent from my iPadŽ

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Yumin
    Code:
             char textfile[3][10]={"file1.txt","file2.txt","file3.txt"};
             for(j=0; j<3; j++)
    	 {
               if((fp=fopen("textfile[j]", "r"))==NULL) 
    	   {cout<<"can not open file "<<j<<endl;
                return;
    	   }
             for(i=0; i<3; i++) fscanf(fp,"%f", &arr[j][i]); 	   
            }
    This codes also can pass compiling and linking, but the result
    of excuting is allways: "can not open file[0]"!
    "textfile[j]" is a string, not a variable. Try
    Code:
    if ((fp = fopen(textfile[j], "r")) == NULL)
    Also, formatting with more whitespace would make the code easier to read:
    Code:
    char textfile[3][10] = {"file1.txt", "file2.txt", "file3.txt"};
    for (j = 0; j < 3; j++)
    {
        etc...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Hi Waltp:

    Thank you for the suggestion, it works.

    By the way how to put all the files into one separate
    folder, say "ABC". You know they are now put in the same
    folder of source codes.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Add the directory (the proper term for folder ) to the filename:

    Code:
    char textfile[3][10]={"ABC\\file1.txt","ABC\\file2.txt","ABC\\file3.txt"};
    Use \\ to add a single backslash since it's read as an ESCape character.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    30
    Hi WaltP:

    I tried your suggestion:

    Code:
        char textfile[3][10]={"ABC\\file1.txt","ABC\\file2.txt","ABC\\file3.txt"};
    repeatedly, it doesn't work! It always prompts:

    error C2117: 'abc\file1.txt' : array bounds overflow

    Where put the folder "ABC", parellel/same-level to source-code-folder or
    sub-folder of of it?

    Have you tested? Sorry bothering you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read keyb in uninterrupted loop
    By flywheel in forum C Programming
    Replies: 11
    Last Post: 01-15-2009, 01:16 PM
  2. read files
    By malebo in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2008, 12:25 PM
  3. Read In From Many Files In One Function
    By djwicks in forum C Programming
    Replies: 12
    Last Post: 03-24-2005, 07:39 AM
  4. Replies: 3
    Last Post: 05-05-2004, 05:40 PM
  5. how to read and write files in c++
    By kerick in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2002, 09:41 PM