I have been working on this for some time and need some help. I have posted to another forum with no results. I figured i would try here and see if i can get some real help. I am trying to populate an array with the contents of a file using fgets. I have a text file with 4 lines to test out my program and my program only prints the 4th line and then a new line and exits. I wrote a mini chat and am going turn it into an FTP server. So here is my data handler section

for( ; ; ) 
{
int size, count = 0;
FILE *fd;
fd = fopen(argv[3], "r");

fseek(fd, 0, SEEK_END);
size = ftell(fd);
rewind(fd);

        if(fd == NULL) 
        {
        printf("%s. FD(NULL)\n", strerror(errno));
        exit(EXIT_FAILURE);
        }
        	else if(fd)
		{	
			while(count < size)
			{
			count++;
			
				while((fgets(buffer, sizeof(buffer), fd)) != NULL)
				{				
				//fgets(buffer, sizeof(buffer), fd);
				}
			}
				
				writefd = write(sockfd, buffer, strlen(buffer));
	
				        if(writefd <= 0)
	        			{
	        			printf("%s. WRITE(-1).\n", strerror(errno));
	        			exit(EXIT_FAILURE);
	        			}
		}

if((readfd = read(sockfd, buffer, sizeof(buffer))) < 0);
        {
        fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr));
        printf("%s. READ(c)\n", strerror(errno));
        exit(0);
        }

//Test to see if the buffer is blank. Uncomment to test.
        if(readfd == 0)
        {
        printf("Null buffer. READ(0)\n");
        }

                else 
                {
                fprintf(stdout, "%s", buffer);
                }

}

i would really appreciate any help or guidance. Thanks

My main problem is your lack of consistent formatting and really deep indents. It makes the code very difficult to follow. See this for better formatting techniques.

commented: Reply has no relevance to my post and was not helpful. +0

Fixed the problem. Thanks for the help!

In any case i have figured it out. Thanks for the help! A sloppy approach but i will fix it up.

for( ; ; ) 
{
int count = 0;

FILE *fd;
fd = fopen(argv[3], "r");

fseek(fd, 0, SEEK_END);
int32_t size = (int32_t)ftell(fd);
rewind(fd);

	if(fd < 0)
	{
	printf("%s", strerror(errno));
	exit(EXIT_FAILURE);
	}

        if(fd == NULL)
        {
        printf("%s. FD(NULL)\n", strerror(errno));
        exit(EXIT_FAILURE);
        }
        	else if(fd)
		{

			while(count < size)
			{
			
				if((fread(buffer, sizeof(buffer), 1, fd)) != '\0')
				{					
				fread(buffer, sizeof(buffer), 1, fd);
				}
			
				if (buffer[strlen(buffer) -1] == '\n')						
				buffer[strlen(buffer)-1] = '\0';

			count++;
			}
				
				writefd = write(sockfd, buffer, strlen(buffer));
	
				        if(writefd <= 0)
	        			{
	        			printf("%s. WRITE(-1).\n", strerror(errno));
	        			exit(EXIT_FAILURE);
	        			}
		}

if((readfd = read(sockfd, buffer, sizeof(buffer))) < 0);
        {
        fprintf(stderr, "Error reading message from %s\n", inet_ntoa(cli_addr.sin_addr));
        printf("%s. READ(c)\n", strerror(errno));
        exit(0);
        }

//Test to see if the buffer is blank. Uncomment to test.
        if(readfd == 0)
        {
        printf("Null buffer. READ(0)\n");
        }

                else 
                {
                fprintf(stdout, "%s", buffer);
                }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.