Thread: Reading a line from a file

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Reading a line from a file

    I'm trying to read in a file and store each line in the file into an array? I need some help, I think im doing something wrong



    Code:
    int main()
    
    {
    ifstream input;
    ofstream output;
    int count=0;
    string word;
    char inFileName[20];
    char outFileName[20];
    cout << "Enter the input file name >> ";
    cin >> inFileName;
    cout << "Enter the output file name >> ";
    cin >> outFileName;
    
    
    input.open(inFileName);
    if(input.fail())
    	{
    	cout << "Unable to open " << input << " now exiting." << endl;
    	exit(1);
    	}
    
    
    output.open(outFileName);
    if(output.fail())
    	{
    	cout << "Unable to open " << output << " now exiting." << endl;
    	exit(1);
    	}
    cout << "Enter the number of entry to be read >>";
    cin >> count;
    char temp[count];
    for (int i =0; i <count; i++)
    	{
    	input >> temp[count];
    	count++;
    	}
    for(int i =0; i <count; i++)
    {
    cout << temp[count]<< endl;
    }
    input.close();
    output.close();
    return 0;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    are you getting any errors?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Problem one:

    You cannot create an array of chars with a variable size the way you are trying to do it: char temp[count]; is wrong if the compiler doesn't know what count is (which in your case it doesn't). The solution is to use new:

    char* temp = new char[count];
    // ... All your code that uses temp
    delete [] temp;


    Problem two:

    Inside your for loops, you should be using i, not count as the index to the array.

    Problem three:

    You aren't reading in a line at a time, you are reading a character at a time from the input file. Fix your other two problems first, and then try and figure out this one.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    is their a way to use "getline" and store into an array?

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    sure.

    fin.getline(CstyleStringArray[i], MaxCharToRead, terminatingChar);

    or

    fin.getline(STLstringVector[i], terminatingChar);

    where fin is an open stream capable of reading from file and associated with a file. The other parameter names seem self explanatory.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    i'm still have a lil hard time using getline. All i want to do is read in a file that contains a sentences for each line. And store that stentences into an array. so i can print it out.

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Do you really need to store each line, or just the line the user asks for?

    By the way, a C-style string is an array itself (actually a null terminated character array), but I will just refer to it as a string. If you only need one line, you don't need a separate array of strings. You can just loop through the input file a line at a time until you reach the line that the user asked for.

    This is one way to get a line from a file into a C-style string and then output the string:
    Code:
    char currentSentence[100]; // Maximum size of line is 100 characters.
    input.getline(currentSentence, 100); // Get either 99 characters + null, or the entire line, whichever is first.
    cout << currentSentence << endl;  // Output the string
    If you can understand that code, then you should be able to use it in your program.

    If you do want to store every line as an array of sentences, you will want to use an array of strings. In this case, that would be an array of character arrays. That is more complicated, so I won't give you details on that unless you really need to do it.

    And as always, if you can use the standard string class (std::string) you would probably have an easier time getting this to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 10:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM