Thread: arrays, pleases help!!!

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    20

    arrays, pleases help!!!

    i am trying to open a .txt file into an array and then print it on the screen.

    i managed to do it in c++ but have been told i have to do it in c.

    i think i have managed to load the file but cant display it.

    it probablly quite easy but ive been pulling my hair out over it for days.

    if anyone knows how to do this please let me no.

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    What are the contents of the text file?

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    It is just A-Z with no spaces

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    ok what u need to is to read each char by char and copy them on to the char array. And then print them all.

    Have a look at function fgetc and printf

    With those two function u will be able to finish of the job.
    ssharish2005
    Last edited by ssharish2005; 07-24-2007 at 04:01 PM.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    the simplest way i can think of to do that would probably be:

    Code:
    FILE *fp;
    char string[26]
    int i;
    
    fp = fopen("filename","r");
    
    fscanf(fp,"%s",string);
    
    for(i=0;i<26;i++)
         printf("%c ",string[i]);
    
    fclose(fp);

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    great, ill have a flick through an let you no how i get on, thankyou guys

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Check if fp is NULL before you mess with it, though.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    for(i=0;i<26;i++)
         printf("&#37;c ",string[i]);
    You could have replaced this with just

    Code:
    printf("%s",string);
    ssharish2005

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    ah, cool, ive managed to do this

    Code:
    #include <stdio.h>
    
    int main ()
    {
        
            
            FILE *file = fopen( "key.txt", "r" );
    
            
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                int x;
                
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "&#37;c", x );
    				
                }
            }
    		printf("\n");
            
    		if 
    		
    		
    		
    		fclose( file );
        
    }
    im trying also now to put the loaded txt file into n array, an then take a user input an swap the A's for D's (just an example), i wana make a SSC.
    I was thing i hav to load it in to an array an then make if statements. If you could show me how you put the txt from the file into an array then i should be ok, i think, lol

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    It looks you literaly copied the code, but never understood what it does. When u read the char from a file that is

    Code:
    x = fgetc( file )
    place the read char on to
    Code:
    a[i] = fgetc(file)
    That will store the value on to array

    ssharish2005

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    20
    should i declare the a[i] as a variable. im just starting to learn, dident realise how dificult this can be, lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM