Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If you're not part of the solution, you're part of the precipitate.


computers / comp.lang.c / Re: saving fileXXX.bmp

Re: saving fileXXX.bmp

<utv1e1$28rsc$1@dont-email.me>

  copy mid

https://www.novabbs.com/computers/article-flat.php?id=34791&group=comp.lang.c#34791

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.c
Subject: Re: saving fileXXX.bmp
Date: Tue, 26 Mar 2024 18:42:24 +0100
Organization: A noiseless patient Spider
Lines: 249
Message-ID: <utv1e1$28rsc$1@dont-email.me>
References: <utpl9q$2u0jk$1@i2pn2.org> <utq5qj$2ummn$1@i2pn2.org>
<utq6qd$jtaq$1@dont-email.me> <utrbig$30267$3@i2pn2.org>
<utsfnb$18bvo$1@dont-email.me> <utu3qg$33j67$1@i2pn2.org>
<utu9v8$33qfv$2@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 26 Mar 2024 17:42:25 +0100 (CET)
Injection-Info: dont-email.me; posting-host="12c7675f9c136a1c90617a3403e6be2d";
logging-data="2387852"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1uYbKgeWHKtbvgUzUL/HK"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Firefox/91.0 SeaMonkey/2.53.18.1
Cancel-Lock: sha1:rtZpsFqjRrwKlTrEfh7BtaghFNM=
In-Reply-To: <utu9v8$33qfv$2@i2pn2.org>
 by: jak - Tue, 26 Mar 2024 17:42 UTC

fir ha scritto:
> fir wrote:
>> jak wrote:
>>> fir ha scritto:
>>>> jak wrote:
>>>>> fir ha scritto:
>>>>>> fir wrote:
>>>>>>> i want to save bitmap (when using editor) but i dont wannt
>>>>>>> to pen a dialog for saving ui just wana do quicksave but not replace
>>>>>>> the file already exist so i want to maybe use such scheme i will
>>>>>>> sawe
>>>>>>>
>>>>>>> "painting001.bmp" and if there is such number i will just increase
>>>>>>> the number to 002 if such exist i will use 003 and so on
>>>>>>>
>>>>>>> do yu thing it is standable to use c std lib (and probably just
>>>>>>> fopen fclose to detect if that file already exist of there is a need
>>>>>>> to use some specific windows functions?
>>>>>>>
>>>>>>> i never used it though - though maybe i could becouse code
>>>>>>> that is able to walk on directories and read all files may be handy
>>>>>>> for various practical usage (liek finding something , removing
>>>>>>> duplicates etc)
>>>>>>
>>>>>> the question is if if somoene would work longer and had 1000 bitmaps
>>>>>> in folder if this will not slow down, or still be fast etc...could
>>>>>> check
>>>>>> experimentally but even then i wouldnt be sure if this is kinda
>>>>>> optimal or wastefull way
>>>>>
>>>>> In order not to manage too many differences between compilers you
>>>>> could
>>>>> try this way:
>>>>>
>>>>> #include <stdio.h>
>>>>> #include <limits.h>
>>>>>
>>>>> int main()
>>>>> {
>>>>>      char pref[50] = "bmp_file_",
>>>>>           cmd[1024],
>>>>>           str[PATH_MAX];
>>>>>      int  seq;
>>>>>      FILE *fp, *f;
>>>>>
>>>>>      sprintf(cmd, "c:\\windows\\system32\\cmd.exe /c dir /b /o:-n
>>>>> %s???
>>>>> 2>nul", pref);
>>>>>
>>>>>      if((fp = popen(cmd, "rt")) != NULL)
>>>>>      {
>>>>>          if(fgets(str, PATH_MAX, fp) != NULL)
>>>>>          {
>>>>>              sscanf(str, "%[^0-9]%3d%*", pref, &seq);
>>>>>              sprintf(str, "%s%03d", pref, ++seq);
>>>>>          }
>>>>>          else
>>>>>              sprintf(str, "%s%03d", pref, 1);
>>>>>
>>>>>          pclose(fp);
>>>>>
>>>>>          if((f = fopen(str, "r")) == NULL)
>>>>>          {
>>>>>              if((f = fopen(str, "w")) == NULL)
>>>>>                  printf("cannot create %s", str);
>>>>>          }
>>>>>          else
>>>>>              printf("%s already exist", str);
>>>>>
>>>>>          if(f != NULL) fclose(f);
>>>>>      }
>>>>>      else
>>>>>          printf("cannot open process");
>>>>>
>>>>>      return 0;
>>>>> }
>>>>>
>>>>> This piece of code is only used to give the idea and is not well
>>>>> tested.
>>>>
>>>>
>>>> that is almost for sure bad - its liek running separate console program
>>>> to add two strings or numbers
>>>>
>>>
>>> I knew you would have given a similar answer but if you agree to open
>>> thousands of files to find the last of them, then you could accept that
>>> solution. It does not only do what you say because the system call is
>>> looking for the file for patterns and reverses the order of the list.
>>> All things you should do in your program. In any case, on Windows
>>> systems there are FindFirst/FindNext functions for this type of
>>> operations. Below is an example where I replace the system call with a
>>> function that uses the functions given before. The convenience of the
>>> system call is that on systems *nix works by simply replacing the call
>>> with "/usr/bin/ls -1r .......".
>>>
>>> #include <stdio.h>
>>> #include <limits.h>
>>> #include <stdbool.h>
>>> #include <windows.h>
>>>
>>> bool FindLastOf(char [], char *);
>>>
>>> int main()
>>> {
>>>      char pref[] = "bmp_file_",
>>>           f2find[50],
>>>           str[PATH_MAX];
>>>      int  seq;
>>>      FILE *f;
>>>
>>>      sprintf(f2find, "%s????", pref);
>>>      if(FindLastOf(f2find, str))
>>>      {
>>>          sscanf(str, "%[^0-9]%4d%*", pref, &seq);
>>>          sprintf(str, "%s%04d", pref, ++seq);
>>>      }
>>>      else
>>>          sprintf(str, "%s%04d", pref, 1);
>>>
>>>      if((f = fopen(str, "r")) == NULL)
>>>      {
>>>          if((f = fopen(str, "w")) == NULL)
>>>              printf("cannot create %s", str);
>>>      }
>>>      else
>>>          printf("%s already exist", str);
>>>
>>>      if(f != NULL) fclose(f);
>>>
>>>      return 0;
>>> }
>>>
>>> bool FindLastOf(char what[], char *result)
>>> {
>>>      WIN32_FIND_DATA fdF;
>>>      HANDLE hF= NULL;
>>>      bool ret = false;
>>>
>>>      *result = '\0';
>>>      if((hF = FindFirstFile(what, &fdF)) != INVALID_HANDLE_VALUE)
>>>      {
>>>          do
>>>              if(strcmp(fdF.cFileName, result) > 0)
>>>                  strcpy(result, fdF.cFileName);
>>>          while(FindNextFile(hF, &fdF));
>>>          ret = true;
>>>          FindClose(hF);
>>>      }
>>>      return ret;
>>> }
>>>
>>> Not even this piece of code is well tested and is just an example.
>>>
>>> Unfortunately, on the systems where the opendir/readir functions are
>>> available instead of FindFirst/FindFext, the work will be more difficult
>>> because they do not have the receipt for patterns.
>>>
>>
>> im not sure what you do your style is unclear to me esp i found name
>> FindLastOf possibly misleading - what last of it founds?
>>
>>
>> wait a bit maybe i will wrote you how i would od it i got my library
>> sickle.c which is able to read list of those names to container in ram
>> and then operate on this
>>
>
>
> i see i posted it in wrong place so here it should be
>
> > see the code for list (by design i invented working on sickle.c -
> > those name convention for this list is not yet quite clear as i
> > generally variables and arrays wrote lettercase but here this
> > list im not so sure so i used pascals
> >
> >
> > void StrCopyMaxNBytes(char* dest, char* src, int n)
> > {
> >    for(int i=0; i<n; i++) { dest[i]=src[i]; if(!src[i]) break;  }
> > }
> >
> >
> >   //list
> >
> >   const int FileNameListEntry_name_max = 500;
> >   struct FileNameListEntry { char name[FileNameListEntry_name_max]; };
> >
> >   FileNameListEntry* FileNameList = NULL;
> >   int FileNameList_Size = 0;
> >
> >   void FileNameList_AddOne(char* name)
> >   {
> >       FileNameList_Size++;
> >       FileNameList = (FileNameListEntry*) realloc(FileNameList,
> > FileNameList_Size * sizeof(FileNameListEntry) );
> >       StrCopyMaxNBytes((char*)&FileNameList[FileNameList_Size-1].name,
> > name, FileNameListEntry_name_max);
> >       return ;
> >   }
> >
>
> ok so to the addtion of container code above this work
>
>  WIN32_FIND_DATA ffd;
>
>  void ReadDIrectoryFileNamesToList(char* dir)
>  {
>   HANDLE h = FindFirstFile(dir, &ffd);
>   if(!h) ERROR_EXIT("error reading directory");
>
>   do  {
>    if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
>      FileNameList_AddOne(ffd.cFileName);
>   }
>   while (FindNextFile(h, &ffd));
>
>  }
>
>
>
> int main(void)
> {
> //  ReadDIrectoryFileNamesToList("C:\\*");
>   ReadDIrectoryFileNamesToList("*");
>
>   for(int i=0; i< FileNameList_Size; i++)
>       printf("\n %d %s", i, FileNameList[i].name );
>
>   return 'ok';
> }
>
> so i got all teh names in list

ok. Your code creates a list with all the file names. If I run mine,
however, it looks for the alphabetically greater file with
"bmp_File_????" search-pattern, acquires the Counter from the name and
increases it to give the name to the next file, then creates it. So if
you run the program in an empty directory the first time it will create
a file called "bmp_file_0001" and every time you run the file in the
same directory it will create a new file with the counter increased.

bmp_File_0001
bmp_File_0002
bmp_File_0003
bmp_File_0004
bmp_File_0005
and so on...

SubjectRepliesAuthor
o saving fileXXX.bmp

By: fir on Sun, 24 Mar 2024

26fir
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor