Thread: in desperate need of help

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    6

    in desperate need of help

    I am to write a program that reads in a command line entry of multiple file names. An example of the prompt would be

    "Enter the names of the files to be used: "

    the user input would be:

    file1.a file2.a file3.a file4.a .... and so on, all on one line, separated by a space.

    The problem i am having is that i know what to do to read one file only, not multiple files. i think the code is

    int i;
    FILE *fp

    for(i=1; i < argc[i]; i++)

    but the trouble i am having is, where would i place the prompt, how would i scan in all the files from the one line, and so on

    If anybody could help, it would be GREEEEEEEEATLY appreciated. Thank you in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you know how to open a single file, then simply call fopen for that file name. If you don't, go see this post.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Not
    Code:
    for(i=1; i < argc[i]; i++)
    but
    Code:
    for(i=1; i < argc; i++)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    notice how your thread got deleted. dont make duplicate posts, it wont help you whatsoever.

    are you trying to extract command line arguments? if so, see vart's post.

    you dont ask for the input if your reading in the filenames from the command arguments.

    if this doesnt help be more detailed and post more code if need be.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    the program itself is run on a unix system.

    when the program starts it asks the user for the files to be linked, and the user types out all the files on one line, and the program is supposed to be take those files. but am i supposed to use a fscanf or fopen in the for loop, or outside the loop, and also, do the files get stored in an array or what, those are the 2 things that are really confusing me right now.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but am i supposed to use a fscanf or fopen in the for loop, or outside the loop
    You have multiple files, so I would expect fopen to be in a loop. Then if the open succeeds, you have fscanf in another loop:
    Code:
    for ( i = 1; i < argc; i++ ) {
      FILE *in = fopen ( argv[i], "r" );
    
      if ( in != NULL ) {
        /* Read from the file here */
      }
    }
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok it seems a bit more clear now. you dont want to do anything with the argc variable (the command line argument one, if you know what im talking about.

    if you must get all of them from one line you create a char[] of a set size (however this isnt flawless), such as char[256] or char[512] would be more than enough room i imagine. then scanf a string and save it to the char[] you made. use the standard libraries to parse a c-string (char[]) and cut it up when it sees spaces. search the reference on cplusplus.com

    hope it helps good luck


    edit: prelude, i think hes having a problem with reading user input and cutting it up into different strings (filenames) seperated by a space each... dinner time for me so im gonna be gone for a while.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why would he need to parse or "cut" the filenames up out of the command line?

    Doesn't he have a pointer to an array of pointers, that give him that, already in argv[]?

    Adak

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i dont think he wants them from the argument list when the program is called. he said he wants to prompt the user for it, meaning a printf and scanf

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    Yeah, it reads the files entered from one line, but i'm still having trouble with where to place the printf statement and do i use fscanf to read the user input, or fopen, or fgets. also, would that be within the for loop or before it, here is what i have so far
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    
    int main(int argc, char *argv[]){
    
    	int i;
    
    	FILE *in;
    	printf("Please enter the files to be used: ");
    	scanf("%p",&argv[i]);
    	for(i=1;i< argc; i++){
    		
    		FILE *in = fopen(argv[i], "r");
    
    		if(in != NULL){
    			
    		}
    	}
    }
    but all it does is execute the printf, and does not scan the files. Also, would i store the files into an array, and where would i do that at?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't seem to be understanding the purpose of command line arguments. You don't use them as some magical spot you write to. You read from them. They invoke the program like so:
    Code:
    myprog these are all arguments
    Then, "myprog" becomes the first stored argument. "these" is another. "are" is another. "all" is another. "arguments" is the final one entered on the command line.

    If all you want to do is prompt them for some, then just loop until they enter "quit" or something, reading one word at a time, and trying to open it.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    no need for sarcasm or attitude, i'm just asking these questions to better understand what i need to do. And the way you answer questions isn't even that helpful. At least the other poster's give suggestions. and there suggestions actually help, not everybody is a coding genius like u there. and your answer still didn't help.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the way you are using argc isnt correct, and please read my previous post and try to edit it accordingly. post back if you need help.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    6
    would i use the argc in the for loop as argc[i], and i still don't understand the whole taking the user input and loading it into an array. and thanks for helping me out

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    get argvc out of your head... you dont use it in your case.
    change: int main(int argc, char *argv[]){
    to: int main() {

    here is how i would read in a string (up to 512 characters) and save it.:

    char myString[512];
    scanf("%s",&myString);

    you have to then split this ONE string into X number of strings appropriatly. see the reference on cplusplus.com for the c-string library or other libraries. i think theres one that splits up a char[] into other char[]s by splitting it where it finds spaces.

    after we get this sorted out then well get the rest. take things one at a time and think about them, research them, and make your posts detailed enough so we all will understand, thus helping you quicker and more efficiently.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 01:25 PM
  2. Debugging Help - I'm Desperate
    By Tman in forum Game Programming
    Replies: 2
    Last Post: 02-21-2006, 01:39 PM
  3. Desperate Help needed
    By roco090 in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2005, 09:43 PM
  4. I don't like doing this but i'm desperate
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-21-2002, 06:44 PM
  5. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM