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-03-2008, 04:21 AM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
how to scan file in C language


This time I have a big problem (for me) with C language.
I searched in cplusplus.com or in google, and I tried with each function
but I don't solve this following question.
I have a text file with six rows, for example:

# begin file

row1...
row2...

# end file

I want to scan this file in C (not C++) language with stream and string functions.

How can I solve above?

Thanks very much for helping,
Savio
 
Old 10-03-2008, 04:53 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
I want to scan this file in C (not C++) language with stream and string functions.
What is a C stream?
Then there are the general questions.
Is this homework?
What have you already done to try and complete the task?
 
Old 10-03-2008, 07:50 AM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
What is a C stream?
ANSI C interface for accessing to file:
fopen
fread
fscanf
getline
...

Quote:
What have you already done to try and complete the task?
I think this is a forum where someone can post questions.
 
Old 10-03-2008, 08:17 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
ref Stream question, good so there is no confusion then.

Quote:
Originally Posted by shifter View Post
I think this is a forum where someone can post questions.
Sure you can post questions but if someone answers it is another matter, so you have to help people to help yourself. "I tried with each function" What did you try? what did not work? what is your actual problem?

Once again is this homework or not?
 
Old 10-03-2008, 08:31 AM   #5
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
My file is:

# Begin configuration file

OPT=/etc/myprogram/myprogram.log

OPT=/etc/myprogram/myprogram.lock

# End configuration file

I'd like copy /etc/myprogram/myprogram.log in something variable.

My stupid code is:

FILE* conf_file;
char* line = NULL;
int lsize = 0;

conf_file = fopen("/etc/program/myprogram.conf", "r");
fseek(conf_file, 0, SEEK_SET);
while (!feof(conf_file)) getline(line, lsize, conf_file);

I want copy row by row line value for using OPT value in C program.

How can I solve this problem?

Thanks,
Savio

Last edited by shifter; 10-03-2008 at 08:32 AM.
 
Old 10-03-2008, 09:16 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
And you have to do this in "C?" Not Perl? Or Awk?
 
Old 10-03-2008, 09:21 AM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
You still have not told us of the problem (although we can see it) and you failed once again to answer the question
Quote:
Once again is this homework or not?
Does this code compile? what error does it give?
What are the parameters types for the function getline?
How big is the string which you have allocated for the line?
What is the result of fopen?
What is the return value of getline?
 
Old 10-03-2008, 10:29 AM   #8
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
Once again is this homework or not?
No, is not a homework.

Quote:
Does this code compile?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


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

int fd, d;
size_t lsize;
FILE* conf_file;
char * line = NULL;

if (argc == 1) {
printf("\nCommand syntax:\n");
printf("\n\tprogramd [ start | stop ]\n");
printf("\t\tstart: start daemon\n");
printf("\t\tstop: stop daemon\n");
}
else if (strcmp(argv[1], "start") == 0) {
conf_file = fopen("/etc/program/program.conf", "r");
if (conf_file == NULL) perror("File not open");
while (!feof(conf_file)) {
d = getline(line, lsize, "/etc/program/program.conf");
printf("%d\n", d);
printf("%s\n", line);
}
}

return(0);

}

The errors are:

bash-3.1# gcc programd.c -o programd
bash-3.1# ./programd

Command syntax:

programd [ start | stop ]
start: start daemon
stop: stop daemon
bash-3.1# ./programd start
-1
Segmentation fault
bash-3.1#

Last edited by shifter; 10-03-2008 at 10:35 AM.
 
Old 10-03-2008, 11:00 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
You should try upping you warning level and this will give you a hint as to why you are having a problem. In short the function is a GNU extension and not an ansi function and a warning will tell you something like "implict function declaration". Add the following to your code before including stdio.h
Code:
#define _GNU_SOURCE
or via the command line
Code:
 gcc programd.c -ansi -Wall -pedantic -D_GNU_SOURCE
and then think about the questions I already posed.
Quote:
Does this code compile? what error(edit: or warnings [why they are warnings without casts I do not know]) does it give?
What are the parameters types for the function getline?
How big is the string which you have allocated for the line?
What is the result of fopen?
What is the return value of getline?
It may seem to you that I am being a pedantic arse, but hopefully this thread has shown you how you ask a question correctly and therefore get answers which may help you. In addition how you could possibly find the solution for yourself in future.

Last edited by dmail; 10-03-2008 at 11:06 AM.
 
Old 10-03-2008, 11:28 AM   #10
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
OK, thank you very much. The correct code is:

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

#define _GNU_SOURCE

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

size_t lsize = 0;
ssize_t read;
FILE* conf_file;
char* line = NULL;

if (argc == 1) {
printf("\nCommand syntax:\n");
printf("\n\tprogramd [ start | stop ]\n");
printf("\t\tstart: start daemon\n");
printf("\t\tstop: stop daemon\n");
}
else if (strcmp(argv[1], "start") == 0) {
conf_file = fopen("/etc/program/program.conf", "r");
fseek(conf_file, 0, SEEK_SET);
while (!feof(conf_file)) {
while (getline(&line, &lsize, conf_file) != -1) {
printf("%s", line);
}
}
}

return(0);

}
 
  


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
send output of namp scan to CSV file rsmccain Linux - Networking 11 04-18-2009 08:31 PM
How to scan a file with 2 different field separators? cdog Linux - Software 13 02-07-2007 01:38 PM
Scan log file for errors huskerharry Linux - Newbie 7 10-20-2006 01:43 AM
Samba + Vscan cannot auto scan incoming file btx Linux - Security 0 05-31-2006 05:31 AM
scan folder to search file os2 Programming 7 03-03-2005 01:37 PM

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

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