LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I can't enter any value when my program runned (https://www.linuxquestions.org/questions/programming-9/i-cant-enter-any-value-when-my-program-runned-4175546793/)

NoWeDoR 06-30-2015 03:55 AM

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;
}


NevemTeve 06-30-2015 04:10 AM

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);


HMW 06-30-2015 04:10 AM

The picture link doesn't work. Can you describe in words what your problem is, and what you expected please.

NoWeDoR 06-30-2015 04:15 AM

When I run this program, naturally it wants from me a value to enter... But I cannot enter any value

NevemTeve 06-30-2015 04:26 AM

> But I cannot enter any value

What is the obstacle?

NoWeDoR 06-30-2015 04:33 AM

A new link for the picture

http://www.megafileupload.com/8U5g/S..._09%281%29.png

HMW 06-30-2015 04:35 AM

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.

HMW 06-30-2015 04:36 AM

Quote:

Originally Posted by NoWeDoR (Post 5384925)

You're gonna have to work on your screenshot skills buddy, still no picture!

NoWeDoR 06-30-2015 06:26 AM

The problem has been solved. I hadn't expressed the file's name correctly to program.

HMW 06-30-2015 08:24 AM

Quote:

Originally Posted by NoWeDoR (Post 5384959)
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!
        }



All times are GMT -5. The time now is 01:29 AM.