Thread: printing string from file

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Iowa City
    Posts
    5

    printing string from file

    I'm trying to print out a 16 by 17 character array that I've included from a text file. I am able to open the file okay. However, instead of printing the character array, the program prints a series of smiley faces. Do you know how I can get my string to print? I appreciate any input. Thanks, Cynthia

    Code:
    char hor_wordsearch[17][17];
    char ver_wordsearch[16][17];
    int i=0,j=0;
    
    	FILE *myFile;
    	myFile = fopen("hor_wordsearch.txt", "r+");
    	if (myFile == NULL)
           {
    		printf("Unable to open hor_wordsearch.txt.\n");
    		return(-1);
    	}
    	printf("Opened hor_wordsearch.txt for reading.\n");
    	
        //I am able to open the text file without difficulty, but I'm having trouble initializing and printing my arrays
    for (i=0; i<16; ++i)
        {
        hor_wordsearch[i][16]= '\0';
        ver_wordsearch[i][16]= '\0';
        }
    while (!feof(myFile))
        {
        fgets(hor_wordsearch[i], 17, myFile);
        printf("%s", hor_wordsearch);
        }
    fclose(myFile);

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while (!feof(myFile))
        {
        fgets(hor_wordsearch[i], 17, myFile);
        printf("%s", hor_wordsearch);
        }
    Here you read each string into the same hor_wordsearch ( i never changes and is out of bounds )
    Kurt

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Iowa City
    Posts
    5
    I don't know how to fix it. Can you give me a hint. Does it involve i++?

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Iowa City
    Posts
    5
    I've revised the code:
    Code:
    while (!feof(myFile))
        {
        fgets(hor_wordsearch[i], 17, myFile);
        printf("&#37;s", hor_wordsearch[i]);
        }
    But I still need to increase i. I'm not sure how to do that.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You might find reading this useful.
    Last edited by mike_g; 07-15-2007 at 01:26 PM.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Location
    Iowa City
    Posts
    5
    Zuk and Mike, It works! Thanks so much for the help--I really appreciate it!

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would try something like this
    Code:
    i = 0
    while (fgets(hor_wordsearch[i], 17, myFile)) {
        printf("%s", hor_wordsearch[i]);
        ++i;
    }
    Kurt

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I would write

    fgets(hor_wordsearch[i], sizeof hor_wordsearch[i], myFile)

    to avoid using magic numbers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM