Thread: error while taking input using file

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Question error while taking input using file

    I'm using Dev-Cpp IDE.

    I'd like to take input from a file. so, I wrote the following input function for my program.
    Code:
    int input (void)
    {    
        int i, j;
        FILE* ip_file;
        
        ip_file = fopen("d:\sudoku_solver\input.txt","r+");
        for (i=0;i<size;i++)
        {
            for (j=0;j<size;j++)
            {
                fscanf(ip_file,"%d",&sudoku[i][j]);
                //copy to TempArr
                sudoku[i][j]==TempArr[i][j];
            }
        }
    }// input()

    my input file looks like this:
    PHP Code:
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000
    000000000 
    The problem is, when I compile the code. i am not getting errors but when I run.. the command prompt is closing saying "encountered error, needed to close"...

    what shall I do.

    This same happens when I take input file at command line.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, you should check the return value of fopen. It's possible you can't open the file for some reason (typo in the file name, permissions, etc). If fopen fails, it returns null, so you should print a useful error message and exit:
    Code:
    int input (void)
    {    
        int i, j;
        FILE* ip_file;
         
        ip_file = fopen("d:\sudoku_solver\input.txt","r+");
        if (ip_file == NULL) {
            perror("Couldn't open input file");
            // return some error code
    }
    Also, you need to return an integer from input. Your compiler should be screaming at you for this. If not, turn the warnings all the way up. If you don't return an integer, you're in the realm of undefined behavior and anything could happen.

  3. #3
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Your input file has no spaces or new lines in-between the 0s so
    Code:
    000000000 
    000000000 
    000000000 
    000000000 
    000000000 
    000000000 
    000000000 
    000000000 
    000000000
     


    is not being translated as the amount of 0s you want. An integer is a number, not a digit. It's reading the entire line of 0s as one number. Space them out, or put each of them on separate lines.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Rodaxoleaux View Post
    is not being translated as the amount of 0s you want. An integer is a number, not a digit. It's reading the entire line of 0s as one number. Space them out, or put each of them on separate lines.
    Good catch.

    @suryak: You should always check the return value of scanf. It will return the number of successful conversions. In your case, you only ask for one, "%d", so you would do:
    Code:
    if (fscanf(ip_file,"%d",&sudoku[i][j]) == 1) {
        //copy to TempArr
        sudoku[i][j]==TempArr[i][j];
    }
    else {
        perror("fscanf failed on input file");
        // return error
    }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    How is TempArr defined?
    '==' is wrong here, it's a typical newbie mistake.
    Last edited by BillyTKid; 11-01-2011 at 03:18 PM.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @anduiril462 : you are right! there is some error in opening the file.

    This is my code..
    Code:
        ip_file = fopen("D:\sudoku_solver\input.txt","r+");
        if (ip_file == NULL) 
        {
                    perror ("couldn't open input file, please check addr\n");
                    system("pause");
        }
    actually, there is a file exactly and I verified the address but it still not able to take it..

    this is error i am getting
    PHP Code:
    12:21 D:\sudoku_solver\io.[Warningunknown escape sequence '\s' 
    12:21 D:\sudoku_solver\io.[Warningunknown escape sequence '\i' 
    where the things might have been wrong

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to escape the backslashes in your string literal, i.e., "D:\\sudoku_solver\\input.txt". Otherwise, it looks like an escape sequence for \s and \i instead of backslash character literals in a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Thanks.. problem is resolved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Taking input from text file..
    By Passa in forum C Programming
    Replies: 4
    Last Post: 09-14-2010, 06:12 PM
  2. Help with taking input from a file
    By babe20042004 in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2009, 08:25 AM
  3. Taking input in C
    By GUIPenguin in forum C Programming
    Replies: 1
    Last Post: 04-12-2006, 01:53 PM
  4. taking user input as title for output file...
    By perkins in forum C++ Programming
    Replies: 4
    Last Post: 12-26-2005, 06:36 PM
  5. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM

Tags for this Thread