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 07-03-2003, 03:38 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
C in Linux. The program only read the first line and then stop


The program only read the first line and then stop

==============================
# Samba SMB password file
public:501:85DBEE24101CEFF8AAD3B435B51404EEB51013B2730E1F16DF6DB7C3A73AD60:[U ]:LCT-3AB622A2:
admin:501:2B251B4003C6F043CA53DE06F7962527:C2D7C320A09B649A91AEC96D4CC32662:[UX ]:LCT-3E21AEC3:

==============================
root:~# ./getdata2
single_char = #
single_char =
single_char = S
single_char = a
single_char = m
single_char = b
single_char = a
single_char =
single_char = S
single_char = M
single_char = B
single_char =
single_char = p
single_char = a
single_char = s
single_char = s
single_char = w
single_char = o
single_char = r
single_char = d
single_char =
single_char = f
single_char = i
single_char = l
single_char = e
single_char =

=================================
#include <stdio.h>

read_smbpasswd_file()
{
FILE *file_pointer, *fopen();
char user_name[32];
char single_char;
int i = 0;

memset(user_name, '\0', 32);
file_pointer = fopen("/etc/samba/smbpasswd", "r");

single_char = getc(file_pointer);
while(!feof(file_pointer))
{
if (single_char != ':')
{
user_name[i] = single_char;
printf ("single_char = %c\n", single_char);
i++;
single_char = getc(file_pointer);
}
else
printf ("user_name[] = %s", user_name);
}
fclose (file_pointer);
}

/******************************************/

main()
{
read_smbpasswd_file();
}
 
Old 07-03-2003, 04:31 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Code:
#include <stdio.h>

int main(void)
{
  FILE *in;
  char buffer[255];
  int n;

  if((in = fopen("smbpasswd","r")) == NULL) {
    perror("Couldn't open file");
    return 1;
  }

  fgets(buffer, 255, in);
  n = strlen(buffer) - 1;

  if(n < 1)
    printf("nothing read from file\n");
  else {
    if(buffer[n] == '\n') buffer[n] = '\0';
    printf("%s\n", buffer);
  }
 
  fclose(in);
  return 0;
}
A bit more efficient, and also checks for errors. (=

Last edited by TheLinuxDuck; 07-03-2003 at 04:35 PM.
 
Old 07-03-2003, 04:40 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
The last line is printed twice

I ran the new code but the last line is printed twice.

===========================
root:~# ./getdata3

# Samba SMB password file
public:501:85DBEE24101CEFF8AAD3B435B51404EEB51013B2730E1F16DF6DB7C3A73AD60:[U ]:LCT-3AB622A2:
user_name[] = public
admin:501:2B251B4003C6F043CA53DE06F7962527:C2D7C320A09B649A91AEC96D4CC32662:[UX ]:LCT-3E21AEC3:
user_name[] = admin

The last line below was printed twice

admin:501:2B251B4003C6F043CA53DE06F7962527:C2D7C320A09B649A91AEC96D4CC32662:[UX ]:LCT-3E21AEC3:
user_name[] = admin


===========================


int main(void)
{
FILE *in;
char buffer[255];
char user_name[32];
int n;
int i = 0;

if((in = fopen("/etc/samba/smbpasswd","r")) == NULL)
{
perror("Couldn't open file");
return 1;
}

while(!feof(in))

{
fgets(buffer, 255, in);
n = strlen(buffer) - 1;

if(n == 0)
printf("nothing read from file\n");
else
{
if(buffer[n] == '\n')
buffer[n] = '\0';
printf("%s\n", buffer);
}


for (i = 0; i <= 254; i++)
if (buffer[i] != ':')
user_name [i] = buffer[i];
else
{
user_name [i] = '\0';
printf ("user_name[] = %s\n", user_name);
break;
}
}

fclose(in);
return 0;
}

Last edited by Linh; 07-03-2003 at 05:32 PM.
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 04:44 PM

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

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