Thread: Reading Data

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Reading Data

    Hi all, I am a beginner in C , I need help in reading data from a standard input and storing it in array .I need to read sudoku data's in a file name sudoku.txt and store the values in an array.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    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

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up fscanf, fgets, or some of the f-ing functions. RTFM.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    this is what I have come up with ...

    Code:
    int c;
    File *data;
    data=open("sudoku.txt","r");
    c=getc(data);

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ok, so that gets you one character. Go read about arrays, and see if you can put those in an array. (Also, read about loops.)


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    How will the compiler know the path of the file??

    Code:
    data=fopen("sudoku.txt","r");

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You specify it. Otherwise it defaults to where your executable is. You could always RTFM (click that) like I said in my first post.
    Quote Originally Posted by TFM
    DESCRIPTION
    The fopen() function opens the file whose name is the string pointed to
    by path and associates a stream with it.
    If you want to be a programmer, you need to learn to look things up.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by code_lover View Post
    How will the compiler know the path of the file??

    Code:
    data=fopen("sudoku.txt","r");
    No... you have to tell it a fully qualified path to the file... What you have only works if the file is in the current working directory.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Is working now!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
     int c,grid[9][9];
     int i=0,j=0;
     FILE *data;
     data=fopen("sudoku1.txt" ,"r");
     
     while ( fscanf(data, "%d", &c) == 1 ) {
     		if(c==' ')
    		{
    		 continue;
    		} 
    		else if(c=='\n')
    		{
    		 i=0;
    		 j++;
    		}       
            grid[i][j]=c;
    		printf("%d\n",grid[i][j++]);
    		counter++;
    return 0;
    }

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, except you should turn the warnings up on your compiler and pay attention to it's reports...
    I'm betting it's complaining about an implicit declaration of counter ...

    Also there are still logic problems in your code... You really need to print that out the grid values to make sure it's capturing the data correctly...
    Code:
    		printf("grid[%d][%d] = %d\n",i,j,grid[i][j++]);
    'Cause I'm betting you're running way off the end of your grid on the j axis.


    First and most important lesson in programming... "Compiles" does not mean "Works".
    Last edited by CommonTater; 09-09-2011 at 04:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  2. Need help in reading data
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 10-26-2006, 09:31 AM
  3. Reading from an data?
    By Tarento in forum C Programming
    Replies: 11
    Last Post: 06-07-2006, 01:50 AM
  4. Replies: 1
    Last Post: 10-22-2005, 05:28 AM
  5. Replies: 2
    Last Post: 06-16-2005, 10:03 AM