LinuxQuestions.org
Visit Jeremy's Blog.
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 06-30-2015, 03:55 AM   #1
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Rep: Reputation: Disabled
I can't enter any value when my program runned


I wrote a program in C/Linux but as in the picture I have an issue. How can I solve this?
Thanks for helps.

The picture is:

http://www.filedropper.com/screensho...15-06-30112909

My Codes are:
Code:
#include <stdio.h>

int main()
{
	FILE *ptrFILE;
	int lineNumber = 1, choice, i, totalLineNumber;
	char c;

	if ((ptrFILE = fopen("Test.txt", "r")) == NULL)
	{
		printf("File couldn't open..\n\n");
	}
	
	printf("*-* Content of file *-*\n\n");
	c = fgetc(ptrFILE);
	while (!feof(ptrFILE))
	{
		printf("%c", c);
		if (c == '\n')
		{
			lineNumber++;
		}
		c = fgetc(ptrFILE);
	}
	printf("\n\nTotal line number : %d\n\n\n\n", lineNumber);
	totalLineNumber = lineNumber;

	printf("Express the line number which you want to display (Enter positive value for at\nthe beginning or vice versa) : ");
	scanf("%d", &choice);
	printf("\n");
	if (choice > 0)
	{
		lineNumber = 0;
		fseek(ptrFILE, 0L, SEEK_SET);
		c = fgetc(ptrFILE);
		while (!feof(ptrFILE))
		{
			printf("%c", c);
			if (c == '\n')
			{
				lineNumber++;
				if (lineNumber == choice)
				{
					break;
				}
			}
			c = fgetc(ptrFILE);
		}
		printf("\n\n\n");
	}
	else if (choice < 0)
	{
		lineNumber = 0;
		fseek(ptrFILE, 0L, SEEK_SET);
		c = fgetc(ptrFILE);
		while (!feof(ptrFILE))
		{
			if (c == '\n')
			{
				lineNumber++;
			}
			if (lineNumber>totalLineNumber + choice)
			{
				printf("%c", c);
			}
			c = getc(ptrFILE);
		}
		printf("\n\n\n");
	}

	fclose(ptrFILE);
	return 0;
}
 
Old 06-30-2015, 04:10 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,870
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
I can't see the picture. Or the problem.
If you have problem with scanf, then try to debug:

Code:
	int rc;

        fprintf (stderr, "we are before 'scanf' be prepared\n");
        fflush (stderr);
        rc= scanf("%d", &choice);
        fprintf (stderr, "scanf returned %d (expected=1) choice=%d\n",
                 (int)rc, (int)choice);
        fflush (stderr);
 
Old 06-30-2015, 04:10 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
The picture link doesn't work. Can you describe in words what your problem is, and what you expected please.
 
Old 06-30-2015, 04:15 AM   #4
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Original Poster
Rep: Reputation: Disabled
When I run this program, naturally it wants from me a value to enter... But I cannot enter any value
 
Old 06-30-2015, 04:26 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,870
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> But I cannot enter any value

What is the obstacle?
 
Old 06-30-2015, 04:33 AM   #6
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Original Poster
Rep: Reputation: Disabled
A new link for the picture

http://www.megafileupload.com/8U5g/S..._09%281%29.png
 
Old 06-30-2015, 04:35 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I don't understand. When I compile your program it works fine:
Code:
gcc -Wall lqc.c -o lqc
lqc.c: In function ‘main’:
lqc.c:6: warning: unused variable ‘i’
As you see I only get a warning for an unused variable. Also, when I run the program it works fine (although it crashes with a segfault if there is no file to open - but that's not the issue at hand here):
Code:
./lqc 
*-* Content of file *-*

line1
line2


Total line number : 3



Express the line number which you want to display (Enter positive value for at
the beginning or vice versa) : 1

line1
I guess you're gonna have to be more specific about your issue.
 
Old 06-30-2015, 04:36 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by NoWeDoR View Post
You're gonna have to work on your screenshot skills buddy, still no picture!
 
Old 06-30-2015, 06:26 AM   #9
NoWeDoR
Member
 
Registered: Jun 2015
Posts: 32

Original Poster
Rep: Reputation: Disabled
Unhappy

The problem has been solved. I hadn't expressed the file's name correctly to program.
 
Old 06-30-2015, 08:24 AM   #10
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by NoWeDoR View Post
The problem has been solved. I hadn't expressed the file's name correctly to program.
Ok. Good. But you might want to change this:
Code:
	if ((ptrFILE = fopen("Test.txt", "r")) == NULL)
	{
		printf("File couldn't open..\n\n");
	}
Into something like this:
Code:
	if ((ptrFILE = fopen("Test.txt", "r")) == NULL)
	{
		printf("File couldn't open..\n\n");
                return 1; // <- You should exit the program here to avoid segfault and other nasty stuff!
	}
 
  


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
Whe runned separately,works! but combined: can't run terminal! ivahbee Linux - Newbie 2 10-31-2014 12:42 AM
Compiled OpenVPN returns a Segmentation Fault when runned on an ARM Board Berris.Oliver Linux - Networking 7 02-23-2012 10:06 AM
Unable to enter password in terminal to enter as root.. ANISHKUMAR.R Linux - Newbie 5 08-07-2010 07:46 AM
Check if program is running, if not start it and enter a password... easy, isn't it? darkrad Linux - Server 11 07-17-2008 12:35 PM
I did the following program and get blank line after pressing <ENTER> purpleburple Programming 5 08-21-2002 12:00 PM

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

All times are GMT -5. The time now is 04:53 AM.

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