LinuxQuestions.org
Help answer threads with 0 replies.
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 04-30-2004, 11:13 PM   #1
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
separating strings


Help. This code works fine under Linux, but will not work correctly when compile with MS Visual 6.0

Any reason for not working correctly????


#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>

int main()
{
FILE* fddata, *fdresults;
char filename[80];
char buffer[80];
char firstname[40],lastname[40];
char* modified_user;

printf("Enter the Name for the file: ");
scanf("%s", filename);
printf("\n");

if((fddata=fopen(filename,"r")) == 0){
perror("Cannot open file ");
return 1;
}
if((fdresults=fopen("users.txt","w")) == 0){
perror("Cannot create file ");
}

if((modified_user=(char*)malloc(sizeof(char)*80)) == NULL){
printf("Cannot allocate memory\n");
return 2;
}
while ( ! feof(fddata)){

fgets(buffer,80,fddata);

if ( feof(fddata)){ break;}
strcpy(firstname,strtok(buffer," "));
strcpy(lastname,strtok(NULL," "));

strcat(modified_user,firstname);
strcat(modified_user,".");
strcat(modified_user,lastname);
}
printf("%s \n",modified_user);
fputs(modified_user,fdresults);

free(modified_user);
fclose(fddata);
fclose(fdresults);

printf("\n");
return 0;
}
 
Old 05-01-2004, 12:30 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Well, I'm not quite sure why you're always allocating 80 bytes for modified_user. If you always allocate the same amount of memory why not just make modified_user a char array?

But I can see a huge buffer overflow problem here. modified_char is only 80 bytes big. Your first problem is that you're not initializing the allocated memory. So your first strcat() call isn't necessarily starting at modified_user+0. Right now (if you're lucky and modified_user[0] is a NUL) modified_user is just going to keep getting longer and longer for each name. So each time it loops, modified_user will grow like this:

first1.last1\n
first1.last1\nfirst2.last2\n
first1.last1\nfirst2.last2\nfirst3.last3\n

(note: for a Windows text file you'll have a bunch of \r\n in your string instead of \n)
and so on...until you print the result which will be some gigantic monster. But that will never happen because I'm sure your 80 bytes of allocated space for modified_user will have long since overflowed if you have any kind of significant number of names.

Anyway, adding modified_user[0] = '\0'; right after your malloc() call will probably fix your problem (as long as 80 bytes is enough to hold the entire list). You didn't explain the symptoms you're seeing and I didn't actually test your code.

Let me know if it still doesn't work and I'll try looking for something in there that's not so obvious.

P.S. Please wrap [ code][ /code] tags around code when you paste it here so indents are kept. It makes it a lot easier to read.

Last edited by itsme86; 05-01-2004 at 12:40 AM.
 
Old 05-01-2004, 12:57 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Here's code that I think does what you're trying to do:

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
  char *buffer, *b, filename[80];
  FILE *fddata, *fdresults;
  struct stat buf;

  printf("Enter the Name for the file: ");
  scanf("%s", filename);
  printf("\n");

  if(!(fddata = fopen(filename, "r")))
  {
    puts("Cannot open file");
    return 1;
  }
  if(!(fdresults = fopen("users.txt", "w")))
  {
    puts("Cannot create file");
    return 2;
  }

  stat(filename, &buf);
  if(!(buffer = malloc(buf.st_size)))
  {
    puts("Cannot allocate memory");
    return 3;
  }

  fread(buffer, 1, buf.st_size, fddata);
  fclose(fddata);

  for(b = buffer;*b;b++)
    if(*b == ' ')
      *b = '.';

  puts(buffer);
  fwrite(buffer, 1, buf.st_size, fdresults);
  fclose(fdresults);

  return 0;
}
and here's what I get when I run the code I just pasted:

itsme:~/C$ cat names.txt
first1 last1
first2 last2
first3 last3
first4 last4
first5 last5
first6 last6
first7 last7
itsme:~/C$ ./filechange
Enter the Name for the file: names.txt

first1.last1
first2.last2
first3.last3
first4.last4
first5.last5
first6.last6
first7.last7

itsme:~/C$ cat users.txt
first1.last1
first2.last2
first3.last3
first4.last4
first5.last5
first6.last6
first7.last7
itsme:~/C$
 
Old 05-01-2004, 06:54 PM   #4
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks alot itsme86 for taking the time to explain and correct my program. Initializing the modified_user[0] pointed me in the right direction.
It's been a couple of months I tried to programmed, and being a newbie doesn't help either.

The program you corrected worked fine under linux. Now I have a base to work from. Thanks once again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
separating syslog output by application mscott Linux - Software 0 03-08-2004 11:45 AM
separating words cxel91a Programming 14 04-29-2003 01:05 PM
reason for separating web server and database server hardigunawan Linux - Security 5 01-25-2003 12:45 PM

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

All times are GMT -5. The time now is 03:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration