LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-12-2010, 11:46 PM   #1
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: 1
C - how to open a txt file and then display the data


I am new to c, can somebody help me, i want to open a txt file and then read line by line and display data with first line and label it 1 and then incremented by 1 for each additional line.
 
Old 10-12-2010, 11:52 PM   #2
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
Hi,

This is Linux forum not C forum.
Here you may get solutions for C on Linux or shell scripting or perl php...
 
0 members found this post helpful.
Old 10-12-2010, 11:55 PM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Prayag,

Kindly read this on the top of this page:
Quote:
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.
and think twice before making a first post to the thread because unknowingly you may take the thread off the zero-reply list without a constructive post.
 
Old 10-13-2010, 12:01 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by UltimateDesi View Post
I am new to c, can somebody help me, i want to open a txt file and then read line by line and display data with first line and label it 1 and then incremented by 1 for each additional line.
http://www.mycplus.com/tutorials/c-p...file-handling/

It is always better to search Google before posting here.
 
1 members found this post helpful.
Old 10-13-2010, 12:01 AM   #5
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
Ohh sorry for that!
 
Old 10-13-2010, 01:39 AM   #6
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by anishakaul View Post
http://www.mycplus.com/tutorials/c-p...file-handling/

It is always better to search Google before posting here.

thanks, that helped me a lot

I am stuck on something else now, i tried google and could not find nothing.

Lets say if i type display in command prompt it should display my txt file, how do i compare the input from user input and then do my display of txt statement after that. Like for example i tried this but it doesn't work.

char input[10240];

if ( fgets(input, sizeof(input), stdin)) == "display" )
....
display my txt file now.
 
1 members found this post helpful.
Old 10-13-2010, 01:46 AM   #7
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by UltimateDesi View Post
Lets say if i type display in command prompt it should display my txt file,
Is it compulsory for you to use the display command ?

Quote:
Originally Posted by UltimateDesi View Post
how do i compare the input from user input and then do my display of txt statement after that. Like for example i tried this but it doesn't work.
Do you mean here that you want to take some input from the user and then compare it with your txt file and then check whether they are identical ? Kindly clarify the question !
 
Old 10-13-2010, 02:27 AM   #8
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by anishakaul View Post
Is it compulsory for you to use the display command ?


Do you mean here that you want to take some input from the user and then compare it with your txt file and then check whether they are identical ? Kindly clarify the question !
Purpose of this, is same has gnu history command, but i need make my own that does the same. when you enter history it displays every command you have enter in shell prompt.

i have application, where i can enter commands(Linux), so whatever i type is stored in this txt file, if i enter for example displaytxt, it should open my txt file and display everything i have done in this application.

Last edited by UltimateDesi; 10-13-2010 at 02:31 AM.
 
Old 10-13-2010, 02:31 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by UltimateDesi View Post
not compare with txt file, i have application, where i can enter commands(Linux), so whatever i type is stored in this txt file, if i enter for example displaytxt, it should open my txt file and display everything i have done in this application.
All I can understand by this is that you have a txt file which contains various Linux commands, now you want to read a command from the text file and execute it ?
Sorry the question is still not very clear. Can you demonstrate this with an ACCURATE EXAMPLE ?
 
Old 10-13-2010, 02:32 AM   #10
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by UltimateDesi View Post
Purpose of this, is same has gnu history command, but i need make my own that does the same. when you enter history it displays every command you have enter in shell prompt.
Does this mean you have to implement your own history command ?
 
Old 10-13-2010, 02:43 AM   #11
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Yes, that's correct.

So this application: you can enter any commands you want for example let's say you enter ls, pwd, cd etc. It takes these commands and appends them to a text file called history and then let's say you enter history in application, so now it should open that text where commands were appended and display them on screen.
 
Old 10-13-2010, 02:46 AM   #12
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by UltimateDesi View Post
Yes, that's correct.

So this application: you can enter any commands you want for example let's say you enter ls, pwd, cd etc. It takes these commands and appends them to a text file called history and then let's say you enter history in application, so now it should open that text where commands were appended and display them on screen.
This means you need to implement your own shell, which takes commands like ls, pwd etc., stores them in *some file* and when you execute that file it should display those commands.

Do you exactly need this ?
 
Old 10-13-2010, 02:55 AM   #13
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by anishakaul View Post
This means you need to implement your own shell, which takes commands like ls, pwd etc., stores them in *some file* and when you execute that file it should display those commands.

Do you exactly need this ?
File should be execute if and only if in this shell application you enter string for example "history". And then open text file and display on screen.

It works same way has gnu history. It will display history if and only if you enter string history.
 
Old 10-13-2010, 02:58 AM   #14
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
For creating a shell read and UNDERSTAND THIS: http://www.linuxquestions.org/questi...preter-829929/
 
Old 10-13-2010, 04:25 PM   #15
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
I think i am making this harder then it should be, but for some reason i can't this to work, so i have a while loop that takes a user input in shell application and keeps appending all the commands you type into history.txt but if you type history it should bring up that history txt file and now display whats store in txt file(which is all the commands you been typing)

My problem is the if statement that tests if the user input is history.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {

char input[10240];


while (fgets(input, sizeof(input), stdin)) {
	
	if ( input == "history")  //if user enters history has a string then display the history txt file.
	{
		FILE *fopen(), *fp;
		int c;
		fp = fopen("history.txt","r");
			c = getc(fp) ;
		while (c!= EOF)
			{
				putchar(c);
				c = getc(fp);
			}
		fclose(fp);
	
	}


}
 
  


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
Script for pulling data out of a txt file schapman43 Linux - Newbie 7 08-03-2010 01:07 PM
How can I use Shell script to edit a data at a particular location in a txt file? leena_d Programming 30 02-08-2010 12:43 AM
Using nslookup from data in a .txt file in BackTrack RexRogan Linux - Newbie 1 12-08-2009 02:35 AM
AWK/Perl for extracting data from txt file to numerous other files briana.paige Linux - Newbie 2 05-05-2009 09:53 AM
How to read HTML or TXT file and output the data? koolkicks311 Programming 1 04-20-2007 11:13 PM

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

All times are GMT -5. The time now is 01:50 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