LinuxQuestions.org
Review your favorite Linux distribution.
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-13-2010, 05:39 PM   #16
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled

Hi -

In C, you can only use "==" for primitive types (like an int, float or a single character). For a string, you must use a comparison function like "strcmp()".

Here's an example that might help:
Code:
$ cat history.txt
aaa
bbb
ccc
ddd
Code:
$ cat tmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 80
#define HISTORY_FILE "history.txt"

int
main(int argc, char *argv[])
{

  if (argc != 2)
  {
    printf ("USAGE: enter command (ex. \"history\")\n");
    return 1;
  }

  /* if user enters history has a string then display the history txt file. */
  if (strncmp (argv[1], "history", 7) == 0)
  {
    char input[MAX_LINE + 1];
    int line_no = 0;
    FILE *fp = fopen (HISTORY_FILE, "r");
    if (!fp)
    {
      printf ("ERROR: Unable to open (%s)!\n", HISTORY_FILE);
      return 1;
    }
    while (!feof (fp))
    {
      if (fgets (input, MAX_LINE, fp))
        printf ("%4d %s", line_no++, input);
    }
    fclose (fp);
  }
  else
  {
    printf ("unknown/unsupported command: %s\n", argv[1]);
    return 1;
  }
  return 0;
}
Code:
$ gcc -Wall -pedantic -o tmp tmp.c

$ ./tmp
USAGE: enter command (ex. "history")

$ ./tmp history
   0 aaa
   1 bbb
   2 ccc
   3 ddd
   4
 
1 members found this post helpful.
Old 10-13-2010, 05:42 PM   #17
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by UltimateDesi View Post
I think i am making this harder then it should be,
For us, yes. All of your posts are extremely unclear as to what you are trying to achieve.

Quote:
Originally Posted by UltimateDesi View Post
Code:
	if ( input == "history")  //if user enters history has a string }
  1. What does input evaluate to?
  2. Does that change when the contents of input changes?
  3. What does "history" evaluate to?
  4. Can they ever compare equal?

Hint: You are trying to comapare strings in C. *wink*
 
Old 10-14-2010, 10:28 PM   #18
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by paulsm4 View Post
Hi -

In C, you can only use "==" for primitive types (like an int, float or a single character). For a string, you must use a comparison function like "strcmp()".

Here's an example that might help:
Code:
$ cat history.txt
aaa
bbb
ccc
ddd
Code:
$ cat tmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 80
#define HISTORY_FILE "history.txt"

int
main(int argc, char *argv[])
{

  if (argc != 2)
  {
    printf ("USAGE: enter command (ex. \"history\")\n");
    return 1;
  }

  /* if user enters history has a string then display the history txt file. */
  if (strncmp (argv[1], "history", 7) == 0)
  {
    char input[MAX_LINE + 1];
    int line_no = 0;
    FILE *fp = fopen (HISTORY_FILE, "r");
    if (!fp)
    {
      printf ("ERROR: Unable to open (%s)!\n", HISTORY_FILE);
      return 1;
    }
    while (!feof (fp))
    {
      if (fgets (input, MAX_LINE, fp))
        printf ("%4d %s", line_no++, input);
    }
    fclose (fp);
  }
  else
  {
    printf ("unknown/unsupported command: %s\n", argv[1]);
    return 1;
  }
  return 0;
}
Code:
$ gcc -Wall -pedantic -o tmp tmp.c

$ ./tmp
USAGE: enter command (ex. "history")

$ ./tmp history
   0 aaa
   1 bbb
   2 ccc
   3 ddd
   4

Thank you, this helped me a lot, I am also stuck on something like if user enters !12...it needs to go into my history text file and then go to line 12 and get the string and have it save to a buffer.

!number - will get the line in the history list that is preceded with that number
!! - get the last line from history text file
!string - will get the last command that begins with that string
!$ -
 
Old 10-14-2010, 11:28 PM   #19
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 also stuck on something like if user enters !12...it needs to go into my history text file and then go to line 12 and get the string and have it save to a buffer.
If user enters !12, you just need to :

1. Reset your file pointer to the start of file.
2. Increment your file pointer by 12
3. Read the string character by character till you get a white space.
4. Display it.

Suggestion :
It is very important for you to get a hold on the file pointer manipulations.
 
Old 10-17-2010, 02:47 AM   #20
UltimateDesi
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 1
Can somebody point me in the right direction. Lets say you want line 4 from text history file..how would i go about going into text file and getting that line and saving it to a variable or pointer.? I have read what anishakaul suggested but i still can't figure it out.

example of history.txt might contain

ls
pwd
cd ..
!!
 
Old 10-17-2010, 10:32 AM   #21
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 have read what anishakaul suggested but i still can't figure it out.
Look up on Google the following functions:
Code:
fseek
lseek
ftell
rewind
 
  


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