Like Tree1Likes

trouble reading a text file into an array

This is a discussion on trouble reading a text file into an array within the C Programming forums, part of the General Programming Boards category; hi Im working on another function for my code, it is creating a ftp config file and then copying the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    trouble reading a text file into an array

    hi
    Im working on another function for my code, it is creating a ftp config file and then copying the files to a ftp server,
    so far i have been able to enter the variables for an IP, a User and a password.
    Another function of my code creates a list of files to be copied, what i am trying to do is read this text file and add each line to my ftp config file with get in front of it allowing it to automatically transfer files, however I am having trouble reading this file in and putting it into another file any help would be greatly appreciated.

    that is what I have so far.
    Last edited by Ciaran; 04-04-2011 at 04:49 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    You need to crank your compiler warnings way up, to the maximum level, and make sure you listen to what it says. If that doesn't work, get a new compiler.

    Code:
    	printf("please enter the directory that you media is stored in\n");
    	scanf("%s", &mediadir);
    	printf("please enter the IP of the server\n");
    	scanf("%s", &ip);
    	printf("please enter your username for the server\n");
    	scanf("%s", &username);
    	printf("please enter your password for the server\n");
    	scanf("%s", &password);
    Drop those ampersands. scanf needs an address to store the data. The name of the array suffices as the address of the first element.

    Code:
    	while(fscanf(cpylst, cpyarray) != NULL)
    	{
    		fprintf(ftp, "get %s", cpyarray);
    	}
    Read the docs for fscanf. You're calling it incorrectly (missing a format string), and it doesn't return a pointer/NULL. It returns the number of items converted. I think you really want to use fgets here, but that also requires a 3rd parameter. Read the docs for fgets too.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    I have tried using fgets as followed and am still not getting anywhere
    thanks for the tips about the ampersands
    Last edited by Ciaran; 04-04-2011 at 04:50 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Works fine for me. What do you mean "not getting anywhere". Does the loop terminate but you see no output in the ftp command file? Is there garbage in the file?

    You need to check the return values from your fopen calls. If the file pointers are NULL, print an appropriate error message, probably using perror or errno/strerror. Also, you don't need the "+" in the open mode. Just open the cpylst file for reading only, using "r", and the ftp file for writing, using "w".

    EDIT: If this is anything other than a simple program for your learning purposes, you should look into using a good library that is well tested and documented. The one I've used is cURL, and it has a Windows version. Check it out: http://curl.haxx.se/.
    Last edited by anduril462; 04-04-2011 at 03:13 PM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    visual studio is telling me that I have a "assertion failure" in the expression (str != NULL)
    what ever that means ?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Well, considering the code you posted has no usage of any assert function/macro or any variable called str, I don't know. I'd need to see more code to know for sure.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    memsetting everything to be spaces isn't how you should be clearing your strings. You should be setting them all to nul (0). Or you could have just done it when you declared them:
    Code:
    type array[ X ] = {0};
    Also fscanf doesn't return NULL. It returns the number of items stored.


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

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    that is everything I have
    im also well aware that the comparison function doesn't work either
    thanks for this guys
    Last edited by Ciaran; 04-04-2011 at 04:42 PM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Still don't see a variable called str in your code, or any calls to assert. This must be coming from a call to a library funciton. A quick Google search came up with a few sites pointing at fprintf. Run your code through the debugger line by line, and look for which fprintf call (or other function call) is causing this. Then, via the debugger, see which variable of yours is NULL when it shouldn't be. There are some issues I noticed though:

    Code:
    //Variables used by multiple parts of the program are declared here
    
    char userdir[100]; //user directory variable
    DIR *serverdir; //declares serverdir as directory 
    FILE *sfp;//declares sfp as file pointer for server media list 
    FILE *cfp;//declares cfp as file pointer for client media list 
    struct dirent *ent;//declares ent as a struct
    char clientname[1024];//declare clientname as a string
    char servername[1024];//declare servername as string
    int i=0;//this counts how many entries there are in server file list
    int j=0;//this counts how many time the comparison loop is used
    
    char copyarray[1024];
    Variables used by multiple parts of the program is no excuse for globals. Global variables are evil, and should be avoided at all costs. Be a good boy/girl and declare them in the appropriate function, and pass them around as needed.

    Code:
    int welcome()//this section displays the applications welcome message
    {//start function
    
    	printf("--------------------------------------------------------------------------------");//dsplay text
    	printf("        Welcome to the ICE Systems file sycronisation SERVER application       \n");//dsplay text
    	printf("--------------------------------------------------------------------------------");//dsplay text
    	printf("\n");//new line
    	
    	return 0;//nothing is returned back to main 
    }//end of the welcome message procidure
    Don't comment every line, everybody knows that a { and } are for the start/end of a function or block of code and that printf displays text. Only comment things that aren't obvious. Also, if you don't return anything that will be used, don't return anything at all. Also, if you don't want to take any parameters, explicitly make the param list void:
    Code:
    void welcome(void)
    Code:
    int createfilelist()//this section creates a list of the servers media directory 
    {//start function
    	...
    	sfp=fopen("c:\\ICE Systems\\serverlist.txt", "w+");//opens the file to store media directory
    Check the value of fopen, and stop processing if you can't open the file. You don't need a "+" in the mode since you never read from that file.

    Code:
    			i++;
    Why are you using a global i, especially when you don't initialize it right before? Change your code to call something else first that puts a non-zero value in i and you're screwed.

    Code:
    	
    	return 0;//nothing is returned back to main 
    }// end of function that created media directory
    Make the function take and return void, since you don't use the return value and there are no params.

    Code:
    	sfp=fopen("c:\\ICE Systems\\serverlist.txt", "r");//points server file pointer to server file list 
    	cfp=fopen("c:\\ICE Systems\\clientlist.txt", "r");//points client file pointer to client file list 
    	cpl=fopen("c:\\ICE Stystems\\copylist.txt", "a+");//points copy list file pointer to copy list 
    	stemp=fopen("c:\\ICE Systems\\stemp.txt", "a+");
    	ctemp=fopen("c:\\ICE Ststems\\ctemp.txt", "a+");
    Again, check return values and print errors if you can't open them all. Don't open with a "+" in the mode unless you are definitely reading AND writing.

    Code:
    			fgets(sll1, 1024, sfp);
    			fgets(cll1, 1024, cfp);
    You should use fgets(sll1, sizeof(sll1), sfp). That way, if you change the size of sll1, you won't have to change your fgets call, or any memsets, etc.

    Code:
    						sllarray[a] = slfirstline[1024];
    						...
    						cllarray[b] = clfirstline[1024];
    slfirstline has 1024 elements. That means that valid indexes are 0..1023. 1024 is out of bounds. You can't assign entire arrays that way, if that's what you're trying to do.

    Code:
    					while(c<a, c++)
    					{
    						fprintf(stemp, "%s", sllarray[c]);
    					}
    
    					while(d<b, d++)
    					{
    						fprintf(ctemp, "%s", cllarray[d]);
    					}
    I suspect those fprintf calls are the source of your assert error. cllarray[d] is a char, %s wants a char *, i.e. a string. I think you really need to brush up on arrays and strings. We have a couple tutorials here and here, and you should Google for some more, and do some simpler practice exercises until you have that down pat.

    Code:
    					copyline[1024] = sll1[1024];
    					//-delete first line of server list and make old second line new first line
    Again, your array indexes are out of bounds. I don't know how this is supposed to delete anything.

    Code:
    			memset(sll1, ' ', 1024);//sets all entries in array sll1[1024] to a blank space 
    			memset(cll1, ' ', 1024);//sets all entries in array cll1[1024] to a blank space 
    			fprintf(cpl, "%s", copyline);//prints copyline to the copy list
    			memset(copyline, ' ', 1024);//sets all entries in array copyline[1024] to a blank space
    Again, memset(sll1, ' ', sizeof(sll1). Also, since you seem to be using all these arrays as strings, you should "empty" them by setting them to the null character, '\0', with memset(sll1, 0, sizeof(sll1)), or more simply sll1[0] = '\0'.

    Code:
    int sendfiles()
    {
    	...
    	ftp = fopen("c:\\ftp.txt", "w+");
    	cpylst = fopen("c:\\Ice Systems\\copylist.txt", "r");
    	char *cpyarray[1024];
    For the last bloody time, check the return values! Also, don't use a "+" in the mode of ftp file! You never read from that file in this function.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    thanks so much man

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    You're welcome.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    could i ask that you delete the code i posted on here that you then quoted please ?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know that this is a public forum right? Deleting things you have questions about denies other people with similar questions the chance to learn from your experiences.

    Quzah.
    anduril462 likes this.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    I understand that, I only ask as this was an assignment and I would prefer not to have to re-write everything so I don't look like I have plagiarised anything. It would be greatly appreciated.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You were just given a bunch of stuff to rewrite, so if you don't ignore all the helpful advice you were just given here, then your code shouldn't be a duplicate of the quoted code sections anyway.


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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble reading from txt file
    By ColtsBR in forum C++ Programming
    Replies: 1
    Last Post: 09-17-2010, 11:23 PM
  2. Reading issue, mixed text file
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-22-2009, 04:38 AM
  3. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  4. Reading a file into an array for fast access later
    By matsharp in forum C Programming
    Replies: 10
    Last Post: 08-03-2006, 02:42 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21