Thread: file output problem

  1. #1
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12

    file output problem

    Hey guys, i'm having toubles with this piece of code. I can't seem to get the syntax correct, can someone please help.

    All I want to code to do is open a text file and display 10 integers.

    The next file just includes numbers from 1-10.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
        int num[10];
        
        ifstream a_file ("test.txt");
        
        for(int i = 0; i>10;i++)
        {
                a_file>>num[i];
        }//end for statement
        
        //display all numbers in file
        for(int z = 0; z > 10; z++)
        {
                cout<<num[z]<<"\n"<<endl;
        }//end for loop
        
        cin.get(); //hold screen
    return 0;
    }//end main

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
    for(int i = 0; i>10;i++)
    edit1:
    Code:
    for(int z = 0; z > 10; z++)
    edit2:
    you didn't close your text file

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> you didn't close your text file
    That's done automatically. Fixing the for loops should solve the problem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe even < 10
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    just to let you know, you can reuse i in the second loop
    those declared in the for loop is local to the loop

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. File output problem using ofstream
    By yatta!¿ in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2002, 05:05 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM