reading from a text file into an array and coding.

This is a discussion on reading from a text file into an array and coding. within the C Programming forums, part of the General Programming Boards category; hi everyone. im a little stuck on some homework and just wondering if anybody has any ideas on the best ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Post reading from a text file into an array and coding.

    hi everyone. im a little stuck on some homework and just wondering if anybody has any ideas on the best ways of going about this problem. basically i need to write a bit of code that will ask the user for the location of a text file to read in then ask the user for a number which will be a cipher key (eg if a = 1 b = 2 and the user enters 5 all the letters move 5 along so a would become e) it then needs to output the new encrypted file. i have managed to write a program to read in a text file and print it on screen but i was thinking of putting the users file into an array using fscanf. i will we using visual studio to do this. any help would be greatly apreciated.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    open file1 for reading 
    open file2 for writing
    
    while file <> EOF
          read one char from a file1
               charpfive = char + 5
               write charpfive to file2
          read next char from file1 
    
    close file1
    close file2
    
    delete file1
    remame file2 to file1
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    thanks for your help. i havnt been able to go in to collage because of the snow but as soon as i get a chance il let you know how i get on

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I presume that you perhaps be living in UK if your snow in hehe

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    hello. yes i live in England. where are you? I have finally managed to get into university. the snow is almost gone now though.
    I have a question as I have only recently began my programming course. I have written the first part of the code to read in a text document and show it on the screen. I would like to add another if statment so if the user enters a file destination that does not exist it comes up with an error message rather than crashing the program? could you help please.
    her is what I have got so far:


    #include <stdlib.h>
    #include <stdio.h>
    //a program that reads in a text file chosen by the user character by character and prints it out on the screen by Thomas collyer 01.12.10
    void main ()//defines a program called main
    {
    FILE *file_in;//defines the stream as file in
    char letter, destination[30]; //defines a string for each character and and array for the name of the text file
     
    printf("please enter the file destination you want to read in\n");//gets the user to enter a file
    scanf("%s" , destination);//reads in the destination and stores it in destination variable
    file_in=fopen(destination, "r");// opens and reads the file
    do//reads in the file and prints the file untill the end of file condition is met.
    {

    letter=getc(file_in);//reads each letter in one at a time to the letter variable
    printf("%c" , letter);//prints each letter out on the screen
    }
    while

    (letter != EOF);//checks if has reached the end of the text file
    fclose(file_in);//closes the stream
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    sorry i forgot to put the code in a separate box. here it is again without the comments

    [code]

    #include <stdlib.h>
    #include <stdio.h>

    void main ()
    {
    FILE *file_in;
    char letter, destination[30];
     
    printf("please enter the file destination you want to read in\n");
    scanf("%s" , destination);
    file_in=fopen(destination, "r");
    do
    {

    letter=getc(file_in);
    printf("%c" , letter);
    }
    while

    (letter != EOF);
    fclose(file_in);
    }

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    I already answered this in your other thread! Don't ask the same question in multiple threads!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in a filename from shell
    By Daerogami in forum C Programming
    Replies: 12
    Last Post: 10-25-2010, 06:32 PM
  2. Importing a test file that contains an array
    By JJE in forum C Programming
    Replies: 1
    Last Post: 10-03-2010, 02:49 AM
  3. fread not playing nice; my array is full of zeros
    By voraciousveggie in forum C Programming
    Replies: 14
    Last Post: 03-16-2010, 10:36 PM
  4. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  5. Map Array to File on Disk?
    By Tommaso in forum Game Programming
    Replies: 1
    Last Post: 04-22-2003, 03:44 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21