Infinite loop output problem

This is a discussion on Infinite loop output problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm having trouble with a java program I was tasked with converting to C, the original program take an ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Post Infinite loop output problem

    Hi, I'm having trouble with a java program I was tasked with converting to C, the original program take an in.txt file filled with rougthly a paragraph's worth or words, each on it's own line and outputs to a out.txt file the same words now on consistent lines no longer than 50 chars, however my current code, only adds a space to each line and then repeats the last word forever, causing me many headaches...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
    
    
    int LINE_SIZE = 50;  // this will decide how long our lines can be
    int Position;
    char InputLine[50];
    
    
    //Creates the pointers needed to reference our files
    FILE *FileIn;
    FILE *FileOut;
    
    
    //open the files
    FileIn=fopen("in.txt","r"); 
    FileOut=fopen("out.txt","w");
    
    //check to see if they opened correctly
    if(FileIn==NULL) 
    {
    printf("Error: can't open the input file.\n");
    }
    
    if(FileOut==NULL) 
    {
    printf("Error: can't open the output file.\n");
    }
    
    //reads one line at a time from in.txt
    fscanf(FileIn, "%s", &InputLine); 
    
    
    //I know the "Position != feof(FileIn)" is wrong but I'm unsure as to what should go there, would it be "InputLine != feof(FileIn)"?
    
    	for (Position=1; Position != feof(FileIn); Position++)  
    	{
    
    		if (InputLine == (NULL))  
    
    // I suspect this is also causing some trouble, it's supposed to check to see if i'm at the end of the line, the original program had ("") instead of NULL but that didn't work
    
    		{
    
    			if (Position > 1)
    			{
    			fprintf(FileOut, "\n");
    			}
    
    		fprintf(FileOut, "\n");
    		Position = 1;
    		}
    
    		else
            	{
    //checks to see if my current line is over the 50 char limit
              	if ((Position+(strlen(InputLine))-1) > LINE_SIZE)
              	{
                	fprintf(FileOut, "\n");
                	Position = 1;
              	}
    // print the word to out.txt in the correct format
    		fprintf (FileOut, "%s\n", InputLine);
              
    		Position += (strlen(InputLine));
    
    			if (Position <= LINE_SIZE)
             		{
              		fprintf (FileOut, " ");
              	  	Position++;
    			}
            	}
    
    		fscanf(FileIn, "%s", &InputLine); 
    	}
    
    	fclose(FileIn); 
    	fclose(FileOut);
    I want to learn from my mistakes so please point me in the right direction, I'd rather write the correct code myself instead of having it handed to me

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     Position != feof(FileIn);
    That's wrong. feof shouldn't be used before something has actually been read, and, that return value comparison isn't what you want.
    Code:
    if (InputLine == (NULL))
    Arrays can never be NULL.


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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    ok. but would "InputLine != feof(FileIn)" be any better to check when I've hit the end of the file? (and therefore stop it from repeat the last word over and over again)

    if it helps any here is the original programs code:

    
    Code:
    import java.io.*;
    
    public class Pretty
    {
      public static final int LINE_SIZE = 50;
      
      public static void main(String[] parms)
      {
        BufferedReader fileIn;
        PrintWriter fileOut;
        String inputLine;
        int position;
        
        try
        {
          fileIn = new BufferedReader(new FileReader("in.txt"));
          fileOut = new PrintWriter(new FileWriter("out.txt"));
          
          inputLine = fileIn.readLine();
          position = 1;
          while (inputLine != null)
          {
            if (inputLine.equals(""))
            {
              if (position > 1)
              {
                fileOut.println();
              }
              
              fileOut.println();
              position = 1;
            }
            
            else
            {
              if ((position+inputLine.length()-1) > LINE_SIZE)
              {
                fileOut.println();
                position = 1;
              }
              fileOut.print(inputLine);
              
              position += inputLine.length();
              if (position <= LINE_SIZE)
              {	// add a blank after the current word
                fileOut.print(" ");
                position++;
              }
            }
            
            inputLine = fileIn.readLine();
          }
          
          fileIn.close();
          fileOut.close();
        }
        
        catch (IOException ioe)
        {
          System.out.println(ioe.getMessage());
          ioe.printStackTrace();
        }
      }
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Jonyb222 View Post
    ok. but would "InputLine != feof(FileIn)" be any better to check when I've hit the end of the file?
    No, that actually does not make much syntactical, much less logical, sense.

    This
    Code:
    char InputLine[50];
    can never be tested as NULL unless you reassign the signifier (which would be stupid, and fortunately you do not).

    Vis. the end of the file, fscanf() will return EOF.
    Code:
    while ((fscanf("%s",InputLine))!=EOF)
    Keep in mind that the return value of fscanf IS NOT placed into InputLine.

    And DON'T USE & with %s and a character array.
    Last edited by MK27; 10-08-2009 at 09:41 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    And DON'T USE & with %s and a character array.
    ummm... could an fgets possibly improve my situation?

    Code:
    fgets(InputLine, 49, FileIn);
    and would this be the proper implementation for the rest of the code?
    Code:
    Position = 1;
    	while ((fscanf("%s",InputLine))!=EOF)
    	{
    
    		if (InputLine == (""))
    		{
    
    			if (Position > 1)
    			{
    			fprintf(FileOut, "\n");
    			}
    
    		fprintf(FileOut, "\n");
    		Position = 1;
    		}
    
    		else
            	{
              	if ((Position+(strlen(InputLine))-1) > LINE_SIZE)
              	{
                	fprintf(FileOut, "\n");
                	Position = 1;
              	}
    		fprintf (FileOut, "%s\n", InputLine);
              
    		Position += (strlen(InputLine));
    
    			if (Position <= LINE_SIZE)
             		{
              		fprintf (FileOut, " ");
              	  	Position++;
    			}
            	}
    
    		fscanf(FileIn, "%s", &InputLine); 
    	}
    
    	//The end has come, repent sinners, REPENT!!!
    	fclose(FileIn); 
    	fclose(FileOut);
    
    }
    *edit* in between classes I was able to compile my new version of my code but unfortunatly I receive this:
    59: warning: passing argument 1 of ‘fscanf’ from incompatible pointer type
    Code:
    while ((fscanf("%s",InputLine))!=EOF)
    and when I run it afterwards I receive a bus error

    any ideas on how to proceed?
    Last edited by Jonyb222; 10-08-2009 at 11:24 AM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There are such things as books/manuals. Look up what the arguments to fscanf are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 01:58 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 11:54 AM
  4. Infinite loop problem
    By Xanth in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2005, 12:08 AM
  5. Infinite loop
    By osal in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-08-2004, 05:18 PM

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