LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2009, 04:09 AM   #1
ibabhelix
Member
 
Registered: Sep 2009
Posts: 51

Rep: Reputation: 18
c programming


I ve written a code and there is one small query, how do i stop writing into this text file when i see TER and then continue with rest of the function my code is :
Code:
void ProcessFile(char fileName[])
{
	int i;
	FILE *rfile;
	FILE *wfile;
	char writeFileName[30], readLine[80];

	if ((rfile = fopen (fileName,"r")) == NULL)
	{
		perror(fileName);
		printf("Exiting program!");
		//getch();
		exit(1);
	}
	else
	{
		CutAndPasteString(writeFileName, ".pdb", ".rtf", fileName);
		wfile = fopen (writeFileName, "w");
	
		while(!feof(rfile))
		{
			fgets(readLine, sizeof(readLine), rfile);

			if(readLine[0] == 'A' && readLine[1] == 'T' && readLine[2] == 'O' && readLine[3] == 'M')
			{
				if (CheckForValues(readLine))
				{
					//printf("%c\n", readLine[13]);
					fputs(readLine, wfile);

					if (strlen(readLine) < 80)
					{
						for (i=0;i<(80 - strlen(readLine));i++)
							fputc(' ', wfile);
					}
					fputs("\n", wfile);
				}
			}
else if (readLine [0] == 'T' && readLine[1] == 'E' && readLine[2] =='R' )
					
		}
		fclose(wfile);fclose(rfile);
		
		ReadingFiles0(0, writeFileName, "cp4.txt");
		ReadingFiles1(0, writeFileName, "cpc4.txt");
		ReadingFiles2(0, writeFileName, "cpcp4.txt");
	}
	return;
}
i want to stop writing into the output file after the condition which i ve highlighted in red, n then proceed with function reading file. how do i do that ?
If i give exit (0);
it exits the whole function how do i sort this ?

please help me.
 
Old 10-27-2009, 04:14 AM   #2
vijay_babu1981
Member
 
Registered: Nov 2004
Location: india
Distribution: fedora 10
Posts: 82

Rep: Reputation: 19
break; will break the while loop.
return; can return from anywhere within the function.
 
Old 10-27-2009, 04:17 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Use break to break from your while-loop
Code:
while (...)
{
    if (readLine [0] == 'T' && readLine[1] == 'E' && readLine[2] =='R')
    {
        break;
    }
}
It will then continue with your existing fclose statements.

I also suggest to have a look at the strstr and strcmp functions (depending on the string that contains TER


PS
There is a dedicated programming section at LQ

Last edited by Wim Sturkenboom; 10-27-2009 at 04:52 AM. Reason: added PS
 
Old 10-27-2009, 01:12 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by Wim Sturkenboom View Post
I also suggest to have a look at the strstr and strcmp functions (depending on the string that contains TER
although that would be slower for a small string.
because it has to
*enter the function
*start the internal loop maybe even run too many loops then necessary
*end the internal loop
*return back
where the if(x[0]==?) is just a comparison in memory
 
Old 10-27-2009, 10:31 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
New MSN group: programming fans -- enjoy programming lswhbcb Programming 1 06-29-2007 07:21 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration