Thread: reading from a file + list of strings

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    17

    reading from a file + list of strings

    Hi
    Just wanted to know if anyone could help me with a reading from file problem.
    When the program reads from a text file which is set out like this:

    p
    pbc
    pcb
    b

    it only prints out the conversion of the last string, in this example case "b".
    I'm guessing this is because of the while statement.

    I just wanted to know if it possible to read in all the strings from a file and print out to another file, all the converted strings.

    The only problem is that this was the given method by the teacher. I was told before that this way isnt the correct standard, but the problem is that was the structure we were asked to use.If you can help I would greatly appreciate it.

    Here is my modified code, because this is a coursework assignment and I know a few people in my class use these boards.

    Code:
                    printf("Please enter a file input path ");
    	scanf("%s", &myfile);
    	 
    	file_in=fopen(myfile, "r");
    	
                    while (!feof(file_in))
    	{
    		fscanf(file_in, "%s", array1);
    	}
    	
    	fclose(file_in);
    	
    	length = strlen(array1);
    	strupr(array1);
    	
    	for ( counter = 0; counter < length; counter++ )
           {
    	
    	if ( romanchar[counter] == 'p' )
    	{
    		sum = sum + 3; 
    	}
    	else if ( romanchar[counter] == 'b' )
    	{
    		sum = sum + 4; 
    	}
    	else if ( romanchar[counter] == 'c' )
    	{
    		sum = sum + 5; 
    	}
       }
    	
    	printf("Please enter a file output path ");
    	scanf("%s", &myfile);
                    
                    file_out = fopen(myfile, "w");
    	fprintf(file_out, "%d\n", sum);
    	fclose(file_out);

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You have to think this through before you write code. You're reading in string after string into the same buffer and then only taking the length of the last one you read, and working on that one.

    Some known issues which we probably told you at least some of them before:

    • Any scanf() function that uses &#37;s is unsafe unless it is specified to use a specific size.
    • Using feof() in a control loop will likely cause your program to do weird things. Don't do it. Use the input function's return value as the loop condition.
    • When you open a file with fopen(), check if the returned FILE * is NULL or not. Not doing this will probably result in crashes if something goes wrong.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    scanf("&#37;s", ...) is unsafe as MacGyver mentions. I have links in my signature. Better read them.
    While you're at it, read the faq entry for feof as loop control (also in signature).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM