C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 27th, 2011, 01:26 AM
novice_ann novice_ann is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 novice_ann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 35 sec
Reputation Power: 0
How toC: process files in a loop taking input from stdin

Hi I am new to C. I have to do an assignment where I have to take filenames as input and process each of them in a lop. What I am doing is taking the input in a space separated string (validations still incomplete) using strtok to retrieve individual filenames, putting them in an array, and processing each element of the array.
But when I am runnig the program, it is unable to open the last file inputted, though my debugging messages show that the last element contains the correct filename.

This is what I have got so far:

int main()
{
FILE *file;
int count;
char word[15] = {'\0'};
char query_string[256];
char *v_array[50];
int loop;
char *f_array[256];



printf("Enter the filenames to be searched separated by space\n");
fgets(query_string,sizeof(query_string),stdin);

f_array[0]=strtok(query_string," ");
printf("zeroth element is %s\n",f_array[0]);
if(f_array[0]==NULL)
{
printf("file not found\n");
return 0;
}

for(loop=1;loop<50;loop++)
{
f_array[loop]=strtok(NULL," ");

if(f_array[loop]==NULL)
break;
}

loop=0;



for(loop=0;loop<50;loop++)
{

if(f_array[loop]==NULL)
break;
printf("File %d is %s.\n",loop,f_array[loop]);


if ((file = fopen(f_array[loop], "r")) == NULL) {
printf("f_array[loop] is %i %s\n",loop,f_array[loop]);
fprintf(stderr, "File open error\n");
return(EXIT_FAILURE);
}
while(fscanf(file,"%s",word) == 1) {
printf("word is %s\n",word);
}
printf("closing file %s\n",file);
fclose(file);
}
//end of file name processing


return 0;
}

My input would go like this : text1.txt text2.txt text3.txt

Reply With Quote
  #2  
Old August 27th, 2011, 02:14 AM
SimonB2 SimonB2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 249 SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Days 17 h 55 m 47 sec
Reputation Power: 110
Apart from not putting your code into a code block, nice post!

I'd suggest that you take a closer look at what is actually stored as the third filename - I think if you look closely enough you'll see that it actually contains an added surprise. Try removing the file processing, and just printing the name of each file inside ' ' marks.

Hint: The solution is to add just 2 characters to your source file!!
Comments on this post
novice_ann agrees: Enjoyed your post. Not giving me the exact answer added to the fun.

Reply With Quote
  #3  
Old August 27th, 2011, 04:41 AM
novice_ann novice_ann is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 novice_ann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 35 sec
Reputation Power: 0
Thanks SimonB2. I found that the last filename contains a newline character.

I added this line after my fgets statement:

query_string[strcspn (query_string, "\n" )] = '\0';

This works. Is there any smarter way to do it? You mentioned adding just two characters in the sourcefile.


Also will strcspn work on all platforms if I have string.h included in the program?

Reply With Quote
  #4  
Old August 27th, 2011, 04:50 AM
SimonB2 SimonB2 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 249 SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level)SimonB2 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Days 17 h 55 m 47 sec
Reputation Power: 110
Just change the delim string for strtok from " " to " \n" - make sure you do it for both of them, else it will fail - either when there's only 1 argument passed or when there's several, depending on which strtok you don't change.

Better make that add 4 characters to the source file - I didn't think through the fact that you'd need to change the delim string for both instances of strtok(I only changed it for the second).

[EDIT:] Yup, if you can compile while using strcspn, it'll work. As far as I know it's a part of the stdlib, so will be everywhere, unlike some of the nuggets found in TC's version of conio.h

Last edited by SimonB2 : August 27th, 2011 at 04:55 AM.

Reply With Quote
  #5  
Old August 27th, 2011, 04:58 AM
novice_ann novice_ann is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 novice_ann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 35 sec
Reputation Power: 0
Yep, that worked like a charm. Fantastic!! :-D Thanks a lot.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How toC: process files in a loop taking input from stdin

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap